document

Strings in C :
Overview :
String data type is not supported in C Programming. String is essentially a collection of characters to form
particular word. String is useful whenever we accept name of the person, address of the person, some
descriptive information. We cannot declare strings using string data type in C since there is no such data
type available in C. Instead, an array of type character is used to represent a string. Normally a group of
alphabets enclosed with in double quotes is a string literal in C.
Description :
Strings in C are generally represented by an arrays of characters. The end of the string is marked using a
special character, the null character , which is simply the character with the value 0. (The null character has
no relation except in name to the null pointer . In the ASCII character set, the null character is named NULL.)
It is simply used to denote the end of the string. So all the rules applicable to array is applicable to strings.
11.1 Initialization of a string:
1) String initialization can be carried out in the following ways, similar to that of an array :
The following declaration and initialization creates a string containing the word "Sourcelens". 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 "Sourcelens".
char Name[11] = {'S', 'o', 'u', 'r', 'c', 'e', 'l', 'e', 'n','s','\0'};
2) If a string is initialized similar to an array, then the above statement can be rewritten as follows :
char Name[11]="Sourcelens";
In this case, it is not necessary to 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.
3) Another way to initialize a string is a variation of the above initialization where the size of the array does
not have to be specified.
char Name[]="Sourcelens";
When we declare char as string[], memory space will be allocated as per the requirement during execution of
the program.
11.2 Library functions for string manipulation :
The string.h header file contains various functions for manipulating arrays of characters.Some of the string
functions available are :
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
String Function
strcpy()
strcmp()
strcat()
strlen()
strrev()
Description
String copy function which copies the contents of
the source string to the destination string.
String comparison function which compares two
strings, string1 and string2 and returns a value
=0, if string1 = string2
>0, if string1 < string2.
<0, if string1 > string2.
Concatenates or joins two strings.
String function to find the number of characters in a
string.
String function to reverse the contents of a given
string.
11.3 Array of Strings :
Another common application of two dimensional arrays is to store an array of strings. A string is an array of
characters; so, an array of strings is an array of arrays of characters. Of course, the maximum size is the same
for all the strings stored in a two dimensional array. It is possible to declare a two dimensional character array
of MAX strings of size SIZE as follows:
char Name[MAX][SIZE];
Now, Name is a character array of size MAX * SIZE is created which can store MAX strings of SIZE size each.
Figure 11.1
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
The above figure 11.1 shows a program to initialize a character array with a string and the print the individual
characters in the array along with their position in the array.
Figure 11.2
The above figure 11.1 shows a program to which denotes the functionalities of the library string functions
such as strcpy(), strcat() and the strcmp() functions respectively. The strcpy() function copies the contents of
the source (second argument ) to the destination array(first argument i.e array name). The strcat() function
concatenates the content of the second argument with the first argument and the strcmp() function
performs a character by character comparison of the two strings given as input.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Figure 11.3
The above figure 11.3 shows an example to sort an array of 5 strings using the bubble sort technique. The
program shows the transition of the first iteration in the watch window. Since the first string in the array is
greater than the second string, we use a temporary variable to swap the two values after comparing the two
strings using the strcmp() function.
Figure 11.4
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
The above figure 11.4 shows an output of the sorting function which sorts the given list of strings in the two
dimensional array in the alphabetical order as seen in the watch window. The given array of strings are sorted
with the help of a temporary character array and the bubble sort technique is implemented for sorting.
11.4 UNICODE :
Not all the language has 26 letters. Some languages like Japanese or Chinese have 1000s of characters and so
256 numbers are not enough to represent all of them. All the strings seen so far are character arrays or arrays
of numbers of size 8 bits. ( 1 byte ). 8 bit can only hold up to 2^8 values which is 256 values. So a 16 bit
scheme came in to being called UNICODE. It is the modern way to represent strings. All major OS,
programming languages, support and recommend Unicode instead of 1 byte char including C.
SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)