String LAB 9
String LAB 9
(CLO 3)
13.1 Objective:
Learn how to declare, initialize and use one-dimensional character arrays (string).
13.2 Scope:
The student should know the following:
1. What is string?
2. How to Read and Print Strings.
3. Some important built-in string functions. How to use them?
13.3 Useful Concepts:
What is String?
A string is any sequence of characters enclosed in double quotes. There is no separate data
type for strings as integer, float or double. The string data type can be considered as a char array. The
difference is that a character variable can hold only one character but a string can have more than
one character in a character array. For example, a string called name with 9 characters can be
declared as:
char name [9] = “I like C” ;
The C-style character string originated within the C language and continues to be supported within
C++. This string is actually a one-dimensional array of characters which is terminated by a null
character '\0'. Thus, a null-terminated string contains the characters that comprise the string followed
by a null.
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";
Following is the memory presentation of above defined string in C/C++ −
Actually, you do not place the null character at the end of a string constant. The C++ compiler
automatically places the '\0' at the end of the string when it initializes the array.
Note: Strings are stored as arrays of characters and have a special ‘\0’ termination character called
NULL appended (attached) to them to signify the end of the string.
Note that if the number of characters including the ‘\0’ is more than the size of the array the
results will be unpredictable. However, if the size of the array is more than the number of characters the
extra spaces after the last ‘\0’ character are kept blank and not referred because the string ends at ‘\
0’. So, always make sure that the size of the array is sufficient for the string. For example, the above
declaration would be wrong if we write
char name [8] = “I like C” ;
The size can be ignored also. In that case the size is considered as that of the number of characters
specified in the declaration.
char name [] = “I like C” ;
String Input/Output:
The easiest way to input strings is by using the C++ library function getline(). The
getline()function reads a string of characters entered at the keyboard until you strike the enter key
(carriage return). The carriage return does not become part of the string; instead a null terminator ‘\0’ is
placed at the end.
For example, in the following program fragment
char str[80] ;
cin.getline(str,80);
and if the user enters
Muslim Abbas
and presses the enter key the string variable str has the characters “Muslim Abbas” with ‘\0’
appended at the end but is not displayed.
To display the string, we can use
cout<<str;
The strcmp function compares the string1 to string2 and returns an integer value to
show the status of the comparison. A value of 0 indicates that the two strings are identical. A value of less
than 0 shows that string1 is lexicographically (according to alphabetic ordering) less than string2.
A value of greater than 0 shows that string1 is lexicographically (according to alphabetic ordering)
greater than string2.
strlen(string1);
The strlen function returns an integer equal to the length of the stored string including blanks,
not including the termination character.
strchr(string, ch );
The strchr function searches string for the first occurrence of ch. This function only tells
whether the string contains ch or not and it will not tell the position of the ch in the string if found.
13.4 Examples:
#include <iostream>
using namespace std;
int main()
{
char array[20] = "Mir Zakriya Javed";
cout<<array<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char array[20];
return 0;
}
The output of the program is:
Example 13.3: Write a Program that Inputs a string using cin only.
#include <iostream>
using namespace std;
int main()
{
char array[20];
return 0;
}
Note that the cin can only input single word strings, to receive strings containing spaces use the
getline function.
#include <iostream>
#include <string>
int main () {
return 0;
}
#include <iostream>
#include <cstring>
int main () {
return 0;
}
#include <iostream>
#include <cstring>
int main () {
if (strcmp(a,b) == 0)
cout<<"Entered strings are equal.\n";
else
cout<<"Entered strings are not equal.\n";
return 0;
}
#include <iostream>
#include <cstring>
int main () {
char arr[100];
cout<<"Enter a string to reverse\n";
gets(arr);
strrev(arr);
cout<<"Reverse of entered string is: "<<arr<<endl;
return 0;
}
Hints: - Use two arrays of same size. One is used to take value from user and other is used to
store reverse string.