0% found this document useful (0 votes)
8 views7 pages

Chapters 5

Chapter 5 covers arrays and strings in C++, explaining that arrays are collections of similar data types that simplify programming. It details how to declare both one-dimensional and two-dimensional arrays, as well as the definition and usage of strings, including common string functions like cin.get(), strcpy(), strcmp(), strcat(), and strlen(). The chapter provides examples and syntax for each concept to aid understanding.

Uploaded by

aftabmujeeb786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Chapters 5

Chapter 5 covers arrays and strings in C++, explaining that arrays are collections of similar data types that simplify programming. It details how to declare both one-dimensional and two-dimensional arrays, as well as the definition and usage of strings, including common string functions like cin.get(), strcpy(), strcmp(), strcat(), and strlen(). The chapter provides examples and syntax for each concept to aid understanding.

Uploaded by

aftabmujeeb786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CHAPTER:5

ARRAYS & STRINGS

Q:1: What is arrays?

ARRAYS:

-A set of similar data type is called arrays.

-Arrays are very commonly used in computer programming to provide simple solutions of many
problems.

-Arrays allow programmer to use single variable name to represent a collection of same type of data.

-Array reduce the program size and provide an easy way of handling the list of numbers or strings.

Q:2: How to declare an array?

DECLARATION OF AN ARRAY:

To declare an array in C++ the type of elements, the name of array and the number of elements is
mention in declaration statement.

FOR EXAPLE:

Data type Array name[array size];

Int a[5];

Q:3: What is two dimensional array?

TWO DIMENSIONAL ARRAY:

-It is the combination of columns and rows.

-It is used single variable to represent some type of data in the form of table or matrice.

Q:4: How to declare two deimensional array?

DECLARATION OF TWO DIMENSIONAL ARRAY:

To define two dimensional array in C++ we give data type, array name and the numbers of elements.

The general form of two dimensional array is:

SYNTEX:
Data type Array name[row size][column size];

FOR EXAMPLE:

int a[2][3];

Hera int is a data type and a is a variable name of an array, [2] is the size of row and [3] is the size of
columns. C 0 1 2

R 0 (0,0) (0,1) (0,2)

1 (1,0) (1,1) (1,2)

Q:5: What is string?

STRING:

-A string is groups of characters.

-In C++ character string is stored in one dimensional array of char datatype.

-Each element of character string hold one character.

-All the strings end with special characters known as null characters which is represented by “\0”.

-The null character automatically append at the end of string.

SYNTEX:

char string name[string size];

EXAMPLE:

char stdname[10];

PROGRAM:

- Write a program that read a name and print on the screen.

#include <iostream.h>

using namespace std;

main()

char stdname[20];
int a;

for(a=0;a<=20;a++)

cout<<”Enter the name”;

cin>>stdname;

cout<<stdname;

Some commonly used string functions are cin.get() function, strcpy(), strcat(), strlen(), strcmp().

Q:6:

Why we use string?

COMMONLY USED STRING FUNCTIONS:

-If we want to use string function in our program we have to include a headerfile (<sting.h>) in our
program.

-C++ support large number of string function in the standard library <sting.h>.

1-Cin.get() FUNCTION :

This function is used to read a string from a keyboard that contain a blank space.

SYNTEX:

cin.get (string name, string size);

-It will take two argument, first argument is the name of string variable and the second is the size of
character array.

-The following example will demonstrate the cin.get() function.

EXAMPLE:

cin.get:

It is used for space.

PROGRAM:
#include<iostream.h>

void main()

char stdname[50];

cout<<”Enter the name”;

cin.get(stdname,50);

cout<<stdname;

2-strcpy() FUNCTION:

The strcpy() function is used to copy the content of one string variable to another string variable.

SYNTEX:

strcpy(string2,string1);

-It takes two arguments string one(1), string two(2).

-When it is executing content of string 1 will be copies to string 2.

-The following program will demonstrate the strcpy() function:

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<string.h>

void main();

char string 1[20]=”lahore”;

char string 2[20]=

strcpy(sting2,string1);

cout<<string2;
getch();

OUTPUT:

Lahore

3-strcmp() FUNCTION:

The strcmp() function compares two strings and return an integer value based ot relt of comparison.

The function will return the following integer values based on the result of comparison.

1-It will return(0), if string 1 and string 2 are same.

2-It will return (1) one, if string 1 is greater than string 2.

3-It will return (-1) , if string 1 is smaller than string 2.

SYNTEX:

strcmp(string 1, string 2);

The following program will demonstrate the strcmp function.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<string.h>

Void main()

char string1[10]=”Hello”;

char string2[0]=”Hello”;

int x;

x=strcmp[string 1, string 2];

cout<<x;

}
OUTPUT:

4-strcat() FUNCTION:

The strcat () function is used to join two strings or we can say that it is used for concatenation.

SYNTEX:

strcat(string1, string2);

The following program will demonstrate the scrcat function.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

char string 1[16]=”Hello”;

char string 2[16]=”World”;

cout<<strcat(string1,string2);

5-strlen() FUNCTION:

The strlen is used to return the length of character of string.

SYNTEX:

strlen(string);

The following program will demonstrate the strlen function.

PROGRAM:

#include<iostream.h>

#include<conio.h>
#include<string.h>

void main()

char ch[30];

cout<<”Enter the name=”;

cin>>ch;

cout<<str(ch);

You might also like