2. String
2
“A Collection of characters written in double quotation marks.”
String Declaration:
C++ stores a string as an array of characters.
Syntax:
char array_name[ length ];
String Initialization:
The syntax of String Initialization.
Syntax:
char array_name[ length ] = value;
char: It indicates the type of an array.
array_name: It indicates the name of an array.
length: It indicates the number of memory locations in the array.
value: It indicates the value of initialization in string.
3. String Input
A string value can be input from the user using different ways.
The “cin” object:
The cin object is used to input a string value without any spaces.
Syntax:
cin >> str;
The “cin.getline( )” object:
The cin.getline() object is used to input a string value including spaces.
Syntax:
cin.getline ( str, len );
The “cin.get( )” object:
The cin.get() object is used to input a single character.
Syntax:
cin.get ( ch );
str: Name of string variable.
len: Length of string variable.
ch: Any character.
3
4. String Input: ( Example )#include< iostream >
using namespace std;
void main()
{
char name[ 5 ];
char c;
count << "Enter your Name = ";
// "cin" Object
cin >> name; // Simple input
cout << endl << "Your Name: " << name << endl;
// "cin.getline ( str, len );" Objet
cin.getline( name, 90 ); // Take input including Space
cout << endl << "Your Name: " << name << endl;
// "cin.get ( ch );" Object
cin.get( c ); //Take one character
cout << endl << "Your Name: " << c << endl;
system ( "pause" );
}
4
5. Array Of Strings
Two - dimensional array of characters.
Each row represents one string.
Each character stored in separate index.
Syntax:
char str[rows][cols];
There are two methods for initializing
1. By assigning individual characters
2. By assigning complete string
For Input:
cin>>str[0];
For Output:
cout<<str[0];
5
6. String Functions:
memchr()
Used to search a byte with particular value in buffer.
SYNTAX : memchr( buffer, ch, size);
Memcmp()
Used to compare each successive byte with corresponding
byte.
• SYNTAX : memcmp( buffer1, buffer2, size);
Memcpy()
Used to copy the number of specified characters from 1st
buffer to 2nd buffer and returns the 1st buffer.
SYNTAX : memcpy(buffer2 ,buffer1 ,size); 6
7. String Functions:
• strcat( ):
The strcat() function is used to append a copy of one string to
the end of second string. It also terminates the resulting
string with null character.
Syntax:
strcat ( srt1, str2 );
• strncat( ):
The strncat() function is used to append the specific number
of characters of one string to the end of second string. It also
terminates the resulting string with null character.
Syntax:
strncat ( srt1, str2, n ); 7
8. String Functions:
• strchr( ):
The strchr() function is used to find the first occurrence of
a character in string and returns a pointer to this character.
It returns NULL is character is not found.
Syntax:
strchr ( str, ch );
• strrchr( ):
The strrchr() function is used to find the last occurrence of
a character in string and returns a pointer to this character.
It returns NULL is character is not found.
Syntax:
strrchr ( str, ch ); 8
9. String Functions:
• stricmp( ):
The stricmp() function is used to compare two string character
by character.
Syntax:
stricmp ( str1, str1 );
• Strncmp( )
Use : compare the specified number of character in two string.
Syntax:
strncmp(str1, str 2, n);
• Strcoll( )
Use : Compare two string values the collating sequence
specified by set locale function and indicating the relationship.
Syntax:
strcoll(str1, str 2);
9
10. String Functions:
• Strcpy( )
Use : copy one string to another including the terminating null
character.
Syntax:
Strcpy (str1, str2);
• strncpy( )
Use : copy one string to another including the terminating null
character. It only copies only specified character.
Syntax:
strncpy (str1, str2, n);
• strlen( )
Use: find length of string.
Syntax: strlen (str);
10
11. String Functions:
strstr( ):
The strstr() function is used to find the first occurrence of second
string within first string and returns a pointer to this character.
It returns NULL if occurrence is not found.
Syntax:
strstr ( str1, str2 );
strpbrk( ):
The strpbrk() function locates the first occurrence in the string pointed
to by s1 of any character from the string pointed to by s2. It returns
NULL if occurrence is not found.
Syntax:
strpbrk (const char *s1, const char *s2);
11
12. String Functions:
12
strspn( ):
• The strspn() function compute length of maximum initial
segment of the string pointed by s1 which consist entirely of
character from the string pointed to by s2.
Syntax:
Strspn ( str1, str2 );
strerror( ):
The function takes an error number as parameter and returns a
pointer to error message associated with that error number.
The function can be called with global variable errno declared
in errno.h.
Syntax:
strerror ( errno );
13. String Functions:
• strrev( ):
• The function reverse all the characters in a string except the null character.
• Syntax:
strrev( str );
strset( ):
The function set all the characters in a string to the specified character accept the
null character.
Syntax: strset( str, ch );
strlwr( ):
The function converts all the characters of a string to lowercase.
Syntax:
strlwr( str );
strupr( ):
The function converts all the characters of a string to uppercase.
Syntax:
strupr( str );
13