Arrays of Character & Strings
Arrays of Character & Strings
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
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
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
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];
}
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