Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Chapter 8 : String

8.1 Introduction

Characters are variable represents one or more characters enclosed in single quotation marks, such as a
single letter or symbol. It cannot be used for a string such as "ali". a character should always be referenced
by single quotation marks (' ').Character type variable (char) is capable of saving alphanumeric data, and it
uses 1 Byte for doing it. Alphanumeric symbols are: letters, numbers and special symbols that can be input
via keyboard.

Ex
name
# include <iostream.h> OUTPUT
void main()
{ A A
char name='AB';
cout<<name; // A ( allocate one byte )
}

Ex
#include<iostream.h>
main() name OUTPUT
{
char name='A\n'; AZ
cout<<name; A
cout<<"Z";
}

Ex
# include <iostream.h>
void main()
{
char name='ABC';
cout<<name; // Error character constant must be one or two characters long in function main()
}

Ex
# include <iostream.h> OUTPUT
name
void main()
{ Ali ghonmein
char name; A A
cin>>name; // Ali ghonmein
cout<<name; // print the first character only
}

String : Array of characters that can store non-numerical values that are longer than
one single character and always have a null character ('\0') automatically appended at
the end,the String is a constant pointer point to the first character as pointer .

Note: A char variable represents a single character, such as a single letter or symbol.
It cannot be used for a string such as "ali". a character should always be referenced
by single quotes (' '), while a string by double quotes (" " ) always have a null
character ('\0') automatically appended at the end.
8.2 Declaration and initialization of string

we can declare and initialize the array of char elements called color by either one of these two methods:

1- char color [ ] = {'b','l','u','e','\0'};

In this case we would have declared an array of 5 elements of type char initialized with the characters that
form the word "blue" plus a null character '\0' which specifies the end of the string.
But arrays of char elements have an additional method to initialize their values: using string literals.

2- char color [ ] = "blue";

Double quoted strings (") are literal constants whose type is in fact a null-terminated array of characters. So
string literals enclosed between double quotes always have a null character ('\0') automatically appended
at the end.

Ex
# include <iostream.h>
void main()
name
{
1 2 3 4 5
char name[5]="Ali" ;
cout<<name; // Ali A l i \0
}
Note: cout<<name // print all string not only first character
Note: The panels in gray color represent char elements with undetermined values.

Ex
# include <iostream.h>
void main()
{
char question[] = "Please, Enter your name: "; Please, Enter your name:Ali ghonmein
char greeting[10] = "Hello,"; Hello,Ali!
char yourname [30];
cout << question;
cin >> yourname; // Ali Ghonmein
cout << greeting << yourname << "!";
}

Note: cout << greeting << yourname << "!"; will not printing the whole name ("Ali
ghonmein") but stopped at the terminating null character or first space.To solve this
problem we need to use cin.getline( ) function.

Ex
# include <iostream.h>
void main ()
{
char name = "Ali Mohammad"; // there are errors
cout << name;
}

٢
8.3 cin.getline ( ) function :
To input a line of text into array which take three arguments :
1- Character array ( char_type* s)
2- Length ( size n )
3- Deliminate character ( char_type delim )
Basic template member declarations
cin.getline (char_type* s, size n );
cin.getline (char_type* s, size n, char_type delim );
Ex.
char yourname[30];
cin.getline(yourname,30) // saving only 29 characters in yourname.
Ex.
char yourname[30];
cin .getline( yourname,8,'C'); // ABCDEFZ
cout << yourname ; // print only AB

Ex.
# include <iostream.h>
void main()
{
char question[] = "Please, enter your name: ";
char greeting[10] = "Hello,";
char yourname [30]; Please, enter your name:Ali ghonmein
Hello,Ali ghonmein!
cout << question;
cin .getline( yourname,30);
cout << greeting << yourname << "!";
}
Note: The cin.getline function reads a string up to the first blank space.

Ex. To enter your name and favourite game

# include <iostream.h>
void main ()
{ Please, enter your name:Ali
char name[50], game[50]; Enter your favourite game:football
cout << " Please,Enter your name: "; Ali's favourite game is football
cin.getline (name,50);
cout << "Enter your favourite game: ";
cin.getline (game,50);
cout << name << "'s favourite game is " << game;
}

٣
8.4 String Manipulation Functions

<cstring.h> or <string.h> are a header file that contains many functions for manipulating

char *strcpy( char *s1, char *s2 );


Copies the string s2 into the character array s1(clear content of s1 and change to
s2). The value of s1 is returned.

char *strncpy( char *s1, const char *s2, size_t n );


Copies at most n characters of the string s2 into the character array s1. The value of
s1 is returned.

char *strcat( char *s1, const char *s2 );


Appends the string s2 to the string s1. The first character of s2 overwrites the
terminating null character of s1. The value of s1 is returned.

char *strncat( char *s1, const char *s2, size_t n );


Appends at most n characters of string s2 to string s1. The first character of s2
overwrites the terminating null character of s1. The value of s1 is returned.

int strcmp( const char *s1, const char *s2 );


Compares the string s1 with the string s2. The function will return an integer. This
integer will either be:
Negative if s1 is less than s2,Zero if s1 and s2 are equal,Positive if s1 is greater
than s2.

int strncmp( const char *s1, const char *s2, size_t n );


Compares up to n characters of the string s1 with the string s2.The function will
return an integer. This integer will either be:
Negative if s1 is less than s2,Zero if s1 and s2 are equal,Positive if s1 is greater
than s2.

size_t strlen ( const char *s );


Determines the length of string s. The number of characters preceding the terminating
null character is returned.
-------------------------------------------------------------------------------------------------------

٤
#include <iostream.h>
#include<string.h>
main() strcpy(y,x) = Happy
{ The string in x is : Happy
char x[14 ] = "Happy "; The string in y is : Happy
char y[ 15 ]="New year";
cout<<"strcpy(y,x) = "<<strcpy( y, x )<<endl; // clear content of x and change to y
cout << "The string in x is : " << x << "\nThe string in y is : " << y << '\n';
}

#include <iostream.h>
#include<string.h>
main() strncpy(y,x,2)= Haw year
The string in x is : Happy
{ The string in y is : Haw year
char x[14 ] ="Happy ";
char y[15 ]="New year";
cout<<"strncpy(y,x,2)= "<<strncpy( y, x,2 )<<endl; // copy first 2 characters of x into y
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
}

#include <iostream.h>
#include<string.h>
main()
{ The string in x is: Happy Birthday to You
The string in y is: Happy Birthday to You
char x[ ] = "Happy Birthday to You"; The string in z is: Happy Birthday
char y[ 25 ];
char z[ 15 ];
strcpy( y, x ); // clear content of y and change to contents of x
// if string x have letter less than in y , delete all string in y or part of string
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
// copy first 14 characters of x into z
strncpy( z, x, 14 );
z[ 14 ] = '\0'; // append '\0' to z's contents
cout << "The string in z is: " << z << endl;
}

#include <iostream.h>
#include<string.h> strcat(y,x)= WXYZABCD
main() The string in x is: ABCD
{ The string in y is: WXYZABCD
char x[14 ]="ABCD";
char y[15 ]="WXYZ";
cout<<"strcat(y,x)= "<<strcat(y,x)<<endl;
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
}

٥
#include <iostream.h>
#include<string.h>
strncat(y,x,2)= WXYZAB
main() The string in x is: ABCD
{ The string in y is: WXYZAB
char x[14 ]="ABCD";
char y[15 ]="WXYZ";
cout<<"strncat(y,x,2)= "<<strncat(y,x,2)<<endl;
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
}

#include <iostream.h>
#include<string.h> The string in x is: ABCD
main() The string in y is: WXYZ
{ strcmp(y,x)= 22
char x[14 ]="ABCD";
char y[15 ]="WXYZ";
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
cout<<"strcmp(y,x)= "<<strcmp(y,x)<<endl;
}

#include <iostream.h>
#include<string.h> The string in x is: ABCD
main() The string in y is: ABCD
{ strcmp(y,x)= 0
char x[14 ]="ABCD";
char y[15 ]=" ABCD ";
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
cout<<"strcmp(y,x)= "<<strcmp(y,x)<<endl;
}

#include <iostream.h>
#include<string.h> The string in x is: ABZD
main() The string in y is: ABCZ
{ strcmp(y,x)= -23
char x[14 ]="ABZD";
char y[15 ]="ABCZ ";
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
cout<<"strcmp(y,x)= "<<strcmp(y,x)<<endl;
}

#include <iostream.h>
#include<string.h> The string in x is: ABZD
main() The string in y is: ABCZ
{ strcmp(y,x)= 0
char x[14 ]="ABZD";
char y[15 ]="ABCZ ";
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
cout<<"strncmp(y,x,2)= "<<strncmp(y,x,2)<<endl;
}

٦
In the comparative examples, the resulting number is the difference in the ASCII code for characters that are
comparable.Example of the difference between the letter C and letter Z = 23

#include <iostream.h>
#include<string.h> The string in x is: ABZD123
main() The string in y is: AB 43
{ strlen(x)= 7
char x[14 ]="ABZD123"; strlen(y)= 5
char y[15 ]="AB 43";
cout << "The string in x is: " << x << "\nThe string in y is: " << y << '\n';
cout<<"strlen(x)= "<<strlen(x)<<endl;
cout<<"strlen(y)= "<<strlen(y)<<endl;
}

#include<iostream.h>
#include<string.h>
int main()
{
char name[25]; Please enter your name : ali
char lastname[25]; That's my name too.
char fullname[50]; Your name is 3 letters long
cout<<"Please enter your name: "; Enter your last name: ghonmein
Your full name is ali ghonmein
cin.getline ( name, 25 );
if ( strcmp ( name, "ali" ) == 0 )
cout<<"That's my name too.\n";
else
cout<<"That's not my name.\n";

cout<<"Your name is "<< strlen (name) <<" letters long\n";


cout<<"Enter your last name: ";
cin.getline ( lastname, 25 );
fullname[0]= '\0'; // strcat searches for '\0' to cat after
strcat ( fullname, name ); // Copy name into full name
strcat ( fullname, " " ); // We want to separate the names by a space
strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
cout<<"Your full name is "<< fullname <<"\n";
}

٧
Ex. This program allows you to enter your user name and password(Login system). There are 3 wrong
attempts only.User name is ahmad and the password is hello

# include <iostream.h>
#include<string.h>
#include<conio.h>
int main()
{
char password[25];
char userName[25];
Please enter your user name: ahmad
for(int i=0;i<3;i++) Please enter your password:hello
{ welcome to my system!
cout<<"Please enter your user name : ";
cin>>userName;
cout<<"Please enter your password: ";
cin>>password;
if( strcmp(userName,"ahmad")==0 && strcmp(password,"hello")==0 )
{
cout<<"welcome to my system!";
break;
}
else
{
clrscr(); // clears the current text window we need the header file #include<conio.h
cout<<" invalid user name or password ,Please Try again!\n ";
}
}
}

You might also like