Strings:

A sequence of characters is generally known as a string. Strings are very commonly used in programming languages for storing and processing words, names, addresses, sentences, etc. in C++ language, string is a data type which is an aay of type char.

 

String Constants

We have already used strings in some of our previous examples. Strings are enclosed in double quotation marks as shown in the following printf() statement.

Printf (“%s”, ”I am a student”);

NOTE: printf() statement is an output statement and %s is format specifier. These will be discussed later.

Here I am a student is a string constant. It is stored somewhere in the memory and it cannot be changed just like numeric constants. Each character of a string occupies one byte of the memory and the last character of the string is the character \0. It looks like two characters but it is actually an escape sequence like \n and it is called the null character. \0 stands for a character with a numerical value of zero. In C++ language, all the strings must end with a null character. It is the only way that tells the compiler where the string ends.

 

String variables

Just like numeric variables, C++ uses string variables to store and manipulate strings. We use scanf(), an input statement (will be discussed later), to store a string in a string variable but it has some limitations therefore, we also use gets() and puts() functions for string (will be discussed later). To demonstrate this let us consider the following program.

#include<iostream>

Void main()string-variables

{

Char yourname[20];

Cout<<”enter your name:”;

Scanf(“%s”,yourname);

Cout<<”hello %s”,yourname”;

}

The program did not print the last name. the reason for not printing it is that scanf() function uses any white space character to terminate entry of a variable. Therefore, it is not possible to enter a multiword string into a single array using scanf().

Notice that we don’t have to enter the null character (\0) while entering the string, instead it is automatically included at the end of the string. This means if your array is 20 character long, you can only store 19 characters in it.

Also notice that in this program there is no address operator(&) preceding the string variable name(yourname). This is because yourname is an address. We have to precede numerical and character variables with the & to change values into the addresses but yourname is the name of an array and therefore, it is already an address and does not need the &.

The gets() and puts() functions:

To solve the problem of multiword strings, C++ uses library functions gets() which stands for get string and its purpose is to get the string from the keyboard and stores it in a string array specified in this function. There is a similar function puts() used with strings and its purpose is to output strings. To understand consider the following fragment of a function:

Puts(“enter your name”)

gets(yourname);

NOTE: these functions will be thoroughly discussed in a later chapter.

There is another function strcmp(). It is used to compare two functions. This function is included in #include<string.h> preprocessor.

The string function compares two strings and returns an integer value based on the comparison. If we assume that string1 is on the left side and string2 is on the right side within the brackets then

strcmp(string1, string2);

will have the following return values with meaning:

less than zero: string1 less than string2

zero: string1 identical to string2

greater than zero: string1 greater than string2

here less than means, if we put string1 and string2 in alphabetical order, one that appears first and vice versa.

There is another function that is used to copy the contents of string1 to string2. E.g.

char name[20];

strcpy(name,”john ibraham”);

here string “john ibraham” would be placed in the array name[].

 

Initializing an array of string

To demonstrate the initialization of an array of names, consider the following declaration:

Char names[5][20]= { “Amjad”, “Qasim”, “Naeem”, “Usman”,”farhan”};

The names in the quotes are already a one dimensional array, therefore, we don’t need braces around each names as we did for two dimensional numeric array. We do need braces around all the names because this is an array of strings. Notice that the  individual names are separated by commas.