Module-III String
Module-III String
We have seen that a string can be represented as a one dimensional character type array. Each
character with in the string will be stored with in one element of the array.
Strings in C are represented by arrays of characters. The end of the string is marked with a special
character, the null character, which is simply the character with the value 0.
Because C has no built-in facilities for manipulating entire arrays (copying them, comparing them,
etc.), it also has very few built-in facilities for manipulating strings. Most C compilers’ includes library
function that allow string to be compared, copied or concatenated (i.e. combined one behind other).
In fact, C's only truly built-in string-handling is that it allows us to use string constants (also called
string literals) in our code. Whenever we write a string, enclosed in double quotes, C automatically
creates an array of characters for us, containing that string, terminated by the \0 character. For
example, we can declare and define an array of characters, and initialize it with a string constant:
char string[] = "Hello, world!";
Two ways to initialize string
In this case, we can leave out the dimension of the array, since the compiler can compute it for us
based on the size of the initialize. This is the only case where the compiler sizes a string array for us,
however; in other cases, it will be necessary that we decide how big the arrays we use to hold strings.
In the above example, a character based array named word is declared, and each element of array is
assigned a character.
The last element is filled with a zero value, to signify the end of the character string (in C, there is no
string type, so character based arrays are used to hold strings).
A printf statement is then used to print out all elements of the array.
String function
Strcmp()
The strcmp function accepts two strings as arguments and returns n integer value depending upon
the relative order of the two strings.
i) A negative value if the 1st string precedes the second string alphabetically.
ii) A value of zero if the 1st string and the second string are identical.
iii) A positive value if the 2nd string precedes the 1st string alphabetically.
Strcmp(string1, string2);
For example
Strcmp(“their”, “there”);
It will return a value of -9 with is the numeric difference between ASCII ‘i’ and ASCII ‘r’
i-r= -9
i.e. 73-82= -9
main()
{
char s1[100], s2[100];
gets(s1);
gets(s2);
if(strcmp(s1,s2)= =0)
printf(“equal \n”);
else if (strcmp(s1,s2)<0)
printf(S1 less than S2]n);
else
printf(S1 greater than S2);
}
strcpy()
Strcpy() function accepts two strings as arguments. It copies the value of string2 to string1.
Strcpy(sting2, string1);
main()
char source[ ]=”Password”;
char dest[30];
strcpy(dest, source);
printf(“\n source string =%s”, source);
printf(“destination string = %s\n”, dest);
}
Strlen()
Strlen() function find the lenth of a sting.
main()
char name[10];
int len;
printf(“ \n enter your name”);
scanf(“%s”,name);
len=strlen(name);
printf(“length of the string is =%d\n”, len);
}
Strcat()
Strcat() function use to concatenates two string.
main()
char source[ ]=”Mishra”;
char dest[30]= “Jyoti”;
strcat(dest, source);
printf(“\n source strinf =%s”, source);
printf(“string after concatenation = %s\n”, dest);
}
Arrays of string
An array of string is a two dimensional array
For example
Initializes 5 names in an array of strings and print them
#include<stdio.h>
const int num=5;
const int length=9;
main()
{
int j;
char name[num][length]={“Tej”,“ Raj”, “Amit”,”Anand”, “Prakash”};
for(j=0;j<num;j++)
printf(“%s\n”, name[j]);
}