Manual # 13
Manual # 13
Topic:
Strings
Subject:
Introduction to Computing
Author:
Engr. Ali Faisal Murtaza
-----------------------------------------------------------------------------------------------------------
-
Just like we studied in arrays of integers, the strings (arrays of characters) will be treated
in the same way that it will need two loops, one for the loading purpose and other for the
displaying purpose.
#include<iostream.h>
int main()
{
int i=0;
char n[6];
for(i=0;i<6;i++)
{
cin>>n[i];
}
cout<<"\n";
for(i=0;i<6;i++)
{
cout<<n[i];
}
cout<<"\n\n";
return 0;
#include<iostream.h>
int main()
{
int i=0;
char n[]={'H','a','S','h','i','m'};
for(i=0;i<6;i++)
{
cout<<n[i];
}
cout<<"\n\n";
return 0;
}
-----------------------------------------------------------------------------------------------------------
-
Involvement of Null Character:
But look in this example, if I did not want to take care of the string through loop, then i
have to involve the null character i.e. '\0'
#include<iostream.h>
int main()
{
int i=0;
char n[]={'H','a','S','h','i','m','\0'};
while(n[i]!='\0')
{
cout<<n[i];
i++;
}
cout<<"\n\n";
return 0;
}
In this program, I’ll increment i and it will be incremented till n[i] found the null
character. The point where it finds the null character, the loop will be terminated and so
string as well.
-----------------------------------------------------------------------------------------------------------
-
But if you did not want to write the null character and also did not want to take care about
the string through loop then write your string like in the below code:
#include<iostream.h>
int main()
{
int i=0;
char n[]="Hashim";
while(n[i]!='\0')
{
cout<<n[i];
i++;
}
cout<<"\n\n";
return 0;
In this kind of string declaration the c++ compiler automatically involves the Null
character by itself.
-----------------------------------------------------------------------------------------------------------
-
But if i want to get rid from these loops, then there is another way that you can use:
#include<iostream.h>
int main()
{
char n[6];
cin>>n;
cout<<n;
cout<<"\n\n";
return 0;
}
When ever you have to write some string then the length of the string will always be from
the length you want + 1, like if you want to type the word like 'world' then the word
'world' contains five words so the length of the string be 6, one space should be reserved
for the null character.
So for that you have to include the header file and also you will use the keyword 'gets'
like in the code below:
#include<iostream.h>
#include<stdio.h>
int main()
{
char n[6];
gets(n);
cout<<n;
cout<<"\n\n";
return 0;
Again you have to make the length of the string, n[size you want + 1 for the null
character].
-----------------------------------------------------------------------------------------------------------
-
String Library Functions
-----------------------------------------------------------------------------------------------------------
-
1. strcpy(target,source)
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char arr[]="Bamboozled";
char tar[20];
strcpy(tar,arr);
cout<<arr<<endl;
cout<<tar<<endl;
cout<<"\n\n";
return 0;
Note: Remember, the target in which array is copied should be declared of enough size to
hold the copying array.
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char arr[20];
char tar[20];
cin>>arr;
strcpy(tar,arr);
cout<<arr<<endl;
cout<<tar<<endl;
cout<<"\n\n";
return 0;
}
-----------------------------------------------------------------------------------------------------------
-
2. strcat(x,y)
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char arr[20];
char tar[20];
cin>>arr;
cin>>tar;
strcat(tar,arr);
cout<<tar<<endl;
cout<<"\n\n";
return 0;
}
-----------------------------------------------------------------------------------------------------------
-
3. strcmp(x,y)
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char x[20];
char y[20];
cin>>x;
cin>>y;
if(!strcmp(x,y))
{
cout<<"strings are same";
}
cout<<"\n\n";
return 0;
if two strings which are compared, match then it will return false i.e. it will return 0.
if x is greater than y, then a positive number is returned.
if x is less than y, then a negative number is returned.
-----------------------------------------------------------------------------------------------------------
-
4. strlen(s)
#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char arr[]="Bamboozled";
int len1,len2;
len1=strlen(arr);
len2=strlen("lump sum");
cout<<len1<<endl;
cout<<len2<<endl;
cout<<"\n\n";
return 0;
int main()
{
char list[7][10]= {
"baber",
"waleed",
"sameed",
"imran",
"azeb",
"jehanzeb",
"qadeer"
};
char y[10];
int i,j;
cin>>y;
for(i=0;i<7;i++)
{
if(!strcmp(y,list[i]))
{
cout<<"Found";
goto end;
}
cout<<"Not found";
end:
cout<<"\n\n";
return 0;
This 2-D string can hold 7 strings and each string can hold upto 10 characters.
-----------------------------------------------------------------------------------------------------------
-
#include<iostream.h>
int main()
{
int i=0;
char n[]="Hashim";
while(n[i]!='\0')
{
cout<<n[i];
i++;
}
cout<<"\n\n";
return 0;
}
Do this code, through pointers.
-----------------------------------------------------------------------------------------------------------
-
Write a program which will consider the following data:
Tom : 555-3322
Mary: 555-8976
Ammar : 555-1037
Ikram : 555-4786
Tahir : 555-6431
Shahid : 555-6461