Introduction to string
• Strings are objects that represent sequences of
characters.
• A string is any sequence of characters enclosed in
double-quotes.
• A string is an array of characters whose last character
is \0.
• The last character \0 is called the null-terminating
character. For this reason, a string is said to be null-
terminated.
• The string library ships with a lot of functions used to
perform almost any type of operation on almost any 1
Cont’d…
• The following declaration and initialization create a
string consisting of the word "Hello".
• To hold the null character at the end of the array,
the size of the character array containing the string
is one more than the number of characters in the
word "Hello.“
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
• If you follow the rule of array initialization, then you
can write the above statement as follows:
char greeting[] = "Hello";
2
Cont’d…
In C and C++, strings are created from arrays.
In C++, [] is called the array subscript operator.
3
Cont’d…
#include <iostream.h>
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
4
Cont’d…
• The data type string is a programmer-defined type and is
not part of the C++ language; the C++ standard library
supplies it.
• Before using the data type string, the program must
include the header file string, as follows:
#include <string.h>
• The statement:
string name = "William Jacob";
• declares name to be a string variable and initializes name
to "William Jacob".
• The position of the first character, W, in name is 0; the
position of the second character , i, is 1; and so on.
• That is, the position of the first character in a string 5
Character manipulation function
• Standard Function Library has a numerous set
of string and character handling functions.
• The string function operate on the null
terminated arrays. The header file required for
string function is "<string.h> and for character
use the header file "<ctype.h>".
• Following table lists few Character functions of
C++ Standard function Library.
6
Cont’d…
1. isalnum() is used to check whether the given
argument is alphanumeric character (integer or
uppercase/lowercase alphabets). This function
returns zero when the argument does not have
any of them.
• Syntax:
int isalnum ( int c );
7
Cont’d…
#include <iostream.h>
#include <ctype.h>
int main()
{
char a;
cout << "Enter a alphanumeric character::\n" << endl;
cin >> a;
if(isalnum(a))
{
cout << "Alpha-numeric character";
}
else
{
cout << "Not a Alpha-numeric";
}
return 0; 8
Cont’d…
2. isalpha() function is used to check whether the
given argument is an alphabet. It returns zero when
the argument does not have any of them.
• Syntax:
int isalpha ( int c );
9
Cont’d…
#include <iostream.h>
#include <ctype.h>
int main()
{
char a;
cout<<"Enter a Alphabet: ";
cin >>a;
if(isalpha(a))
{
cout << "Good entered a alphabet\n";
}
else
{
cout << "Not an alphabet\n";
}
return 0; 10
Cont’d…
3. isdigit() function is used to check the character
whether it is between 0-9 or a decimal. It returns
zero when the argument is not a decimal or an
integer in the given range.
• Syntax:
int isdigit ( int c );
11
Cont’d…
#include <iostream.h>
#include <ctype.h>
int main()
{
char a;
cout<< "Enter a digit between 0-9: ";
cin >> a;
if (isdigit(a))
{
cout << "Entered a digit\n";
}
else
{
cout << "Not a digit\n";
}
return 0; 12
Cont’d…
4. isupper() character function is used to check if the
argument contains uppercase letters (A-Z). This
function returns zero if uppercase letters (A-Z) are
not found.
• Syntax:
int isupper( int c );
13
Cont’d…
#include <iostream.h>
#include <ctype.h>
//Example reads in a character and tests if it uppercase
int main()
{
char x;
cin>>x;
if(isupper(x))
{
cout<<"Uppercase";
}
else
{
cout<<"Not uppercase.";
}
}
14
Cont’d…
4. islower() is used to check the function arguments
for lowercase letters (a-z). It returns zero if
lowercase letters (a-z) are not found.
• Syntax:
int islower( int c );
15
Cont’d…
#include <iostream.h>
#include <math.h>
#include <ctype.h>
int checkCase(char x)
{
if
cout << islower (x);
return x;
}
int main ()
{
char ch;
cout << "Enter a character to see if it is lower case:" << endl;
cin >> ch;
checkCase (ch);
cout << "Character "<< ch << " is lower case." <<endl;
return 0;
} 16
Cont’d…
5. isxdigit() is used to check if the argument is a
hexadecimal characters in the range A-F,a-f,or 0-9.
Those are "0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F".
If the argument does not have a them, the function
returns Zero.
• Syntax:
int isxdigit ( int c );
17
Cont’d…
#include <iostream.h>
#include <ctype.h>
int main()
{
char a_char;
cin>>a_char;
if(isdigit(a_char))
{
cout<<"Is a digit!";
}
else
{
cout<<"Is not a digit!";
} 18
String manipulation function
1. strcpy(s1, s2) (string copy) :Copies string s2 into string s1.
(declared in string.h) copies its second argument to its first,
character by character, including the final null character.
2. strcat(s1, s2)( string concatenate);Concatenates string s2
onto the end of string s1.
3. strlen(s1) (string length): (declared in string.h) counts the
characters in its string argument up to (but excluding) the
final null character.
4. strcmp(s1, s2)(string comparison);Returns 0 if s1 and s2 are
19
#include <iostream.h>
Cont’d…
#include <cstring.h>
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
// copy str1 into str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// concatenates str1 and str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// total lenghth of str1 after concatenation
len = strlen(str1);
cout << "strlen(str1) : " << len << endl; 20
#include <iostream.h> Cont’d…
#include <string.h> #include <iostream.h>
void main() #include <string.h>
{
string name = "William Jacob";
int main ()
{
string str1, str2, str3, str4; //Line 2
// Define three string objects.
cout << "Line 3: Name = " << name << endl; //Line 3
string str1, str2, str3;
str1 = "Hello There"; //Line 4
// Assign values to all three.
cout << "Line 5: str1 = " << str1 << endl; //Line 5
str1 = "ABC";
str2 = str1; //Line 6 str2 = "DEF";
cout << "Line 7: str2 = " << str2 << endl; //Line 7 str3 = str1 + str2;
str1 = "Sunny"; //Line 8 // Display all three.
str2 = str1 + " Day"; //Line 9 cout << str1 << endl;
cout << "Line 10: str2 = " << str2 << endl; //Line 10 cout << str2 << endl;
str1 = "Hello"; //Line 11 cout << str3 << endl;
str2 = "There"; //Line 12 // Concatenate a string onto str3 and display
str3 = str1 + " " + str2; //Line 13 it.
cout << "Line 14: str3 += "GHI";
str3 = " << str3 << endl; //Line 14 cout << str3 << endl;
str3 = str1 + ' ' + str2; //Line 15 return 0;
cout << "Line 16: str3 = " << str3 << endl; //Line 16
}
str1 = str1 + " Mickey"; //Line 17
cout << "Line 18: str1 = " << str1 << endl; // 21
Cont’d…
#include <iostream.h> #include<iostream.h>
#include <string.h> #include <cstring.h>
int main () int main()
{
{
char s[] = "ABCDEFG";
string str1 = "Hello"; cout << "strlen(" << s << ") = " << strlen(s) <<
string str2 = "World"; endl;
string str3; cout << "strlen(\"\") = " << strlen("") <<endl;
int len ; char buffer[80];
cout << "Enter string: "; cin >> buffer;
// copy str1 into str3
cout << "strlen(" << buffer << ") = " <<
str3 = str1; strlen(buffer) << endl;
cout << "str3 : " << str3 << endl; }
// concatenates str1 and str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;
// total lenghth of str3 after concatenation
len = str3.size();
cout << "str3.size() : " << len << endl;
return 0; 22
}
Cont’d…
#include <cstring.h>
#include <iostream.h>
int main()
{
char s1[] = "When you come to a fork in the road, ";
char s2[] = "take it. Yogi Berra";
char s3[20];
cout << "\n s1 contains: " << s1 << endl;
cout << " s1 can be copied into s3 \n";
strcpy(s3, s1);
cout << " s3 contains: " << s3 << endl;
strcat(s3, s2); // concatenate s2 to s3
cout << " After concatenation, s3 contains: \t"
<< s3 << endl;
return 0; 23
#include<iostream.h> if(i==0)
#include<conio.h> cout<<"\nBoth strings are equal\n";
#include<string.h> else if(i<0)
void main() cout<<s1<<" is less than "<<s2<<endl;
{ else
char s1[20],s2[20],i; cout<<s1<<" is greater than "<<s2;
clrscr();
cout<<"Enter the string to find the length:"; cout<<"\n\nEnter the string to change into lower
cin>>s1; case:";
cout<<"\nLength of the string is..."<<strlen(s1); cin>>s1;
cout<<"\nLower case of the given string
strcpy(s2,s1); is..."<<strlwr(s1);
cout<<"\n\nCopied string is..."<<s2;
cout<<"\n\nEnter the string to change into upper
case:";
cout<<"\n\nEnter 2 strings to be concatenated:";
cin>>s1;
cin>>s1>>s2;
cout<<"\nUpper case of the given string
strcat(s1,s2);
is..."<<strupr(s1);
cout<<"\nConcatenated string is..."<<s1<<endl;
cout<<"\n\nEnter the string to be reversed:";
cout<<endl<<"\nEnter 2 strings to be
cin>>s1;
compared:";
cout<<"\nThe reversed string is..."<<strrev(s1);
cin>>s1>>s2; 24
getch();
i=strcmp(s1,s2);
Member Functions of the Standard Class string
25
Cont’d…
26
Cont’d…
27
Cont’d…
Assignment 1
swap(str1,str2, str3);
replace(str1 str2);
insert(str1, str2);
find(str1);
erase(str2);
empty();
append(str1, str2);
clear(str1);
28