Você está na página 1de 10

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;

Note: one place of array takes only one character.


So, if you`ll give the input like:
h
a
s
h
i
m
after five inputs it will give you the output: hashim

And if you’ll give the input like:


hash
i
m
then after three inputs it will show you the output: hashim
This is due to the reason that it will consider one character at one place.
-----------------------------------------------------------------------------------------------------------
-
So one can see here that we have to take care of the string through the loop.

#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.

Get String (gets):


now in the above code, if you’ll write 'world' the it works alright but if you’ll write 'he is',
then as space comes between 'he' and 'is' so C++ will show you only he as a output not is.
This is due to the reason that when space comes be 'he' and 'is', then string will take space
as null character and so terminate the string.

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

We mainly use four String Functions:

1. strcpy(target,source): String Copy Function (Copies a String into Another).


2. strcat(x,y): Appends one string at the end of another.
3. strcmp(x,y): String Compare Function (Compares the two strings).
4. strlen(s) : String Length Function (Finds length of a string).
Note: Again you have to include header files stdio.h as well as string.h for these string
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;

Note: Space are aslo included in strings length.


-----------------------------------------------------------------------------------------------------------
-
2D Arrays of Strings
#include<iostream.h>
#include<stdio.h>
#include<string.h>

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

each name has a ticket number.


now if a user entered the name tom, then the program should display the ticket number of
the name. And if the name is not in the list then program display "false name".
-----------------------------------------------------------------------------------------------------------
-
Write a program in which the user will enter any word, and your program should sort it
alphabetically.
-----------------------------------------------------------------------------------------------------------
-

Você também pode gostar