0% found this document useful (0 votes)
59 views5 pages

Arrays of Character & Strings

The document discusses arrays of characters and strings in C++. It provides examples of declaring, initializing, and manipulating character arrays and strings using built-in and user-defined functions. Key points covered include initializing strings, reading strings using cin and gets, comparing strings, and functions for operations like copying, reversing, concatenating, and extracting substrings of strings. Pre-defined string functions from the string.h header file are also explained.

Uploaded by

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

Arrays of Character & Strings

The document discusses arrays of characters and strings in C++. It provides examples of declaring, initializing, and manipulating character arrays and strings using built-in and user-defined functions. Key points covered include initializing strings, reading strings using cin and gets, comparing strings, and functions for operations like copying, reversing, concatenating, and extracting substrings of strings. Pre-defined string functions from the string.h header file are also explained.

Uploaded by

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

Delhi Public School, R.K.

Puram Computer Science

Array of Characters and Strings


Declaration of an array of character
char C[10];
Initialization of an array of character
char Grades[5]={'A','B','C','D','E'};

EXAMPLE 1:
char Grades[5]={'A','B','C','D','E'};
for (int I=0;I<5;I++)
cout<<Grades[I]<<endl;
OUTPUT
A
B
C
D
E

EXAMPLE 2:
char Grades[5]={'A','B','C','D','E'};
int G,Scores[5]={12,5,10,2,9};//Scores out of 12
for (int I=0;I<13;I++)
{
G=4-(Scores[I]+2)/3;
cout<<Scores[I]<<":"<<Grades[G]<<endl;
}
OUTPUT
12:A
5:C
10:A
2:D
9:B

Initialization of an array of characters as a string


char City[10]=”Delhi”;
char University[]=”Delhi University”;
char Landmark[10]={'T','E','M','P','L','E','\0'};

EXAMPLE 3:
char City[10]=”Delhi”;
char University[]=”Delhi University”;
char Landmark[10]={'T','E','M','P','L','E','\0'};
cout<<City<<”,”<<University<<endl;//Displaying content of Strings
cout<<Landmark<<endl; //Displaying content of a String
OUTPUT
Delhi,Delhi University
TEMPLE

C++ Arrays Part 3 CScXI/2016_2017/MK/13 #1


Delhi Public School, R.K.Puram Computer Science

A ​string is an array of character, which has a null character ​\0 termination for the content. In the above
example, the first two initializations, null character was automatically assigned after the last characters
i ​and ​y respectively. Whereas, in third initialization, null character is explicitly assigned to make the
array of characters to become a string.
EXAMPLE 4 EXAMPLE 4a
char Name[30]; int A,B,C;
cout<<”Enter Name:”;cin>>Name; cin>>A>>B>>C;
cout<<Name<<endl; cout<<A<<”:”<<B<<”:”<<C<<endl;
OUTPUT: OUTPUT:
Enter Name: ​Rudra Pratap Singh 120 30 459
Rudra 120:30:459

Look at the above EXAMPLE 4, if “Rudra Pratap Singh” is entered by the user for Name variable using
cin>>​, only the first name “Rudra” will be taken as content of Name and remaining portion of input will
be in buffer. Here, ​Name will be ​a string as a null character will be automatically inserted immediately
after ‘a’ of “Rudra”. EXAMPLE 4a illustrates clearly that cin>> uses space as a separator for inputs.

EXAMPLE 5
char Address[20],City[20],Country[20];
cout<<”Address:”;cin>>Address;
cout<<Address<<endl;
cout<<”City:”;cin>>City;
cout<<City<<endl;
cout<<”Country:”;cin>>Country;
cout<<Country<<endl;
OUTPUT:
Address:​E-40,ABColony Delhi India
E-40,ABColony
City:Delhi
Country:India

In the EXAMPLE 5, if “​E-40,ABColony Delhi India​” ​is entered by the user for Address variable using
cin>>​, only the first portion “​E-40,ABColony​” will be taken as content of ​Address and remaining
portion of input will be in buffer and will automatically assign ​City as “​Delhi​” and ​Country as “​India​”
from input buffer. The program will not ask the user to give inputs for City and Country explicitly.

EXAMPLE 6
char Address[20],City[20],Country[20];
cout<<”Address:”;gets(Address);//gets() requires stdio header file
cout<<Address<<endl;
cout<<”City:”;gets(City);
cout<<City<<endl;
cout<<”Country:”;gets(Country);
cout<<Country<<endl;
OUTPUT:
Address:​E-40,ABColony Delhi India
E-40,ABColony Delhi India
City:​Delhi
Delhi
Country:​India
India

C++ Arrays Part 3 CScXI/2016_2017/MK/13 #2


Delhi Public School, R.K.Puram Computer Science

In the EXAMPLE 6, if “​E-40,ABColony Delhi India​” ​is entered by the user for Address variable using
gets​, now the entire content “​E-40,ABColony Delhi India​” will be taken in ​Address alongwith
spaces. The execution of the program will stop to take inputs for ​City​ and ​Country​ separately.

Important:
● String Variable can’t be assigned with assignment operator =
i.e. char Name[20];
Name=”Preeti”; //is wrong
● String Variables can’t be compared with relational operators ==, <, >, >=, <=, !=
i.e. char MyName[20],YorName[20];
gets(MyName);
gets(YorName);
if (MyName==YorName) //is wrong
cout<<”Both Names are Same”<<endl;

User-defined Function to ​copy content from one User-defined Function to ​compare content of one
string to another string with another
void COPY(char Another[],char One[]) int COMP(char One[],char Two[])
{ {
for (int I=0;One[I]!=’\0’;I++) for (int I=0;One[I]==Two[I] &&
Another[I]=One[I]; One[I]!=’\0’ &&
Another[I]=’\0’; Two[I]!=’\0’;I++);
} return One[I]-Two[I];
}

User-defined Function to find the ​number of User-defined Function to ​concatenate content of


characters​ present in a string one string with another
int LENGTH(char Str[]) void CONCAT(char One[],char Two[])
{ {
for (int I=0;Str[I]!=’\0’;I++); for (int I=0;One[I]!=’\0’;I++);
return I; for (int J=0;Two[J]!=’\0’;J++)
} One[I++]=Two[J];
One[I]=’\0’;
}

User-defined Function to ​reverse the content of a User-defined Function to extract a ​portion


string (substring)​ of a string
void REVERSE(char Str[]) void SUBSTR(char Str[],char Sub[],
{ int Pos,int L)
for (int L=0;Str[L]!=’\0’;L++); {
for (int J=0;J<L/2;J++) for (int I=0,J=Pos;
{ I<L && Str[J]!=’\0’;J++,I++)
char T=Str[J]; Sub[I]=Str[J];
Str[J]=Str[L-J-1];
Str[L-J-1]=T; Sub[I]=’\0’;
} }
}

C++ Arrays Part 3 CScXI/2016_2017/MK/13 #3


Delhi Public School, R.K.Puram Computer Science

Main() function to illustrate the working of the above string OUTPUT


manipulation functions with sample data

void main() Length:6


{ Both Strings Different
char S[30]=”Bharat”,T[30]=”New Delhi”; Changed String:BharatNew Delhi
char R[30],P[30]; New Delhi reversed as ihleD weN
cout<<”Length:”<<LENGTH(S)<<endl; New is portion of
if (COMP(S,T)==0) BharatNew Delhi
cout<<”Both Strings Same”<<endl;
else
cout<<”Both Strings Different”<<endl;
CONCAT(S,T);
cout<<”Changed String:”<<S<<endl;
COPY(P,T);
REVERSE(T);
cout<<P<<” reversed as ”<<T<<endl;
SUBSTR(S,R,6,3);
cout<<R<<” is a Portion of ”<<endl;
cout<<S<<endl;
}

Pre-defined string functions


Header File: string.h
Description Function EXAMPLE OUTPUT
To find length of a string strlen() char Str[]=”Qutab Minar”; 11
cout<<strlen(Str)<<endl;
To copy a string to another strcpy() char S[]=”Qutab Minar”,T[20]; Qutab Minar
strcpy(T,S);
cout<<T<<endl;
To concatenate two strings strcat() char S[20]=”Qutab”,T[]=”Minar”; QutabMinar
strcat(S,T); Minar
cout<<T<<endl<<S<<endl;
To compare two strings strcmp() char Different
S[20]=”Qutab”,T[20]=”Minar”;
if (strcmp(S,T)==0)
To compare two strings strcmpi() cout<<”Same”<<endl
ignoring upper/lowercases else
cout<<”Different”<<endl;
To reverse content of a string strrev() char S[20]=”Qutab”; batuQ
cout<<strrev(S)<<endl; batuQ
cout<<S<<endl;
To convert content of a strupr() char S[20]=”Qutab”; QUTAB
string into uppercase cout<<strupr(S)<<endl; QUTAB
cout<<S<<endl;
To convert content of a strlwr() char S[20]=”Qutab”; qutab
string into lowercase cout<<strlwr(S)<<endl; qutab
cout<<S<<endl;

C++ Arrays Part 3 CScXI/2016_2017/MK/13 #4


Delhi Public School, R.K.Puram Computer Science

Array of Strings
Initialization of an array of Strings
char Cities[3][20]={”Delhi”,”Kolkata”,”Mumbai”};
for (int I=0;I<3;I++)
cout<<Cities[I]<<”:”;
cout<<endl;
OUTPUT
Delhi:Kolkata:Mumbai:

User Defined function to allow user to enter the User Defined function to display the content in an
content in an array of String array of String
void Enter(char S[][20],int N) void Display(char S[][20],int N)
{ {
for (int I=0;I<N;I++) for (int I=0;I<N;I++)
{ cout<<”[“<<I<<”]”<<S[I]<<endl;
cout<<”String[“<<I<<”]?”; }
gets(S[I]);
}
}
User Defined function to search for a string content User Defined function to sort an array of String in
from an array of String ascending order
void Search(char S[][20],int N, void Sort(char S[][20],int N)
char SS[]) {
{ for (int I=0;I<N-1;I++)
int Found=0; for (int J=0;J<N-I-1;J++)
for (int I=0;I<N;I++) if (strcmp(S[J],S[J+1])>0)//1
if (strcmp(S[I],SS)==0) {
{ char T[20];
cout<<”Found..."<<endl; strcpy(T,S[J]);
Found++; strcpy(S[J],S[J+1]);
} strcpy(S[J+1],T);
if (Found==0) }
cout<<”Not Found..."<<endl; }
} //Statement marked with 1 will be
//changed to the following for
//descending order sorting
// if(strcmp(S[J],S[J+1])<0)

main() function to illustrate the working of the above array of string manipulation OUTPUT
functions with sample data

void main() [0]Kolkata


{ [1]Delhi
char Cities[3][20]={”Kolkata”,”Delhi”,”Mumbai”}; [2]Mumbai
Display(Cities,3); Found...
Search(Cities,3,”Mumbai”); Not Found...
Search(Cities,3,”Chennai”); [0]Delhi
Sort(Cities,3); [1]Kolkata
Display(Cities,3); [2]Mumbai
}

C++ Arrays Part 3 CScXI/2016_2017/MK/13 #5

You might also like