String and Character Array

 

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.

 

String variables:

Just like numeric variables, C language uses string variables to store and manipulate strings. But the string data type is not supported by C programming language.

 

Declaring and Initializing string variables:

In C programming language, string variables can be initialized in different ways. Consider the following line of code in which we have initialized a string variable:

char name [12] = “programming”;

This array can also be initialized like the following:

char name [12] = {‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘m’, ‘i’, ‘n’, ‘g’, ‘\0’};

It should be noted here that when we initialize the character array by listing, then the null character must be separately supplied separately in the list as the last character.

 

String Input and Output:

We use scanf () function to store a string in a string variable but it has some limitations therefore, we also use gets() and puts() functions for string. To demonstrate this let us consider the following program:

# include <stdio. h>

# include < conio. h>

void main()

{Output

char yourname [20];

printf (“enter your name:”);

Scanf (“%s”, yourname);

printf (“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 () function.

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 characters long, you can only store 19 characters in it. One character is reserved for the null character (\0).

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 & operator 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 & operator.

 

The gets () and puts () functions:

To solve the problem of multiword strings, C language 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);

 

String Handling Functions:

In C programming language there are many functions for string handling such as strcmp (), strcat (), strcpy (), etc. The string functions are include in the C standard libraray that is <string. h>. This header file must be included in the program to use the string functions.

The most commonly used string functions in C programming language are as follows:

Function Description
strcat () This function is used to join together two strings or we can say that it is used to concatenate two strings.
strlen () This function is used to return the length of the string.
strrev () This function is used to reverse the entered string.
strcpy () This function is used to copy one string into another string variable.
strcmp () This function is used to compare two strings.


Strcmp () function:

The strcmp () 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.

Strcpy () function:

The strcpy () string function is used to copy the contents of string1 to string2. For example:

char name[20];

strcpy (name, “John Abraham”);

Here string “John Abraham” 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]= { “Stuart”, “Bill”, “Faddy”, “Wilson”, “Abraham”};

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 arrays. We do need braces around all the names because this is an array of strings. Notice that the individual names are separated by commas.