String and Related standard functions
Null terminated strings as arrays of characters: In 'C' Language the group of characters, digits, and symbols enclosed within quotation marks are called as string. The string is always declared as character array. In other word character arrays are called string. There is a large difference between an integer array and a character array. In character NULL ('\0') character is automatically added at the end.
For example-
char name [ ] = {'N','E','T','C','O','M','\0'};
Each character of the string occupies 1 byte of memory. The last character is always '\0'. It is not compulsory to write '\0'.the compiler automatically puts '\0' at the end of the string.
Memory map of string-
2001 2002 2003 2004 2005 2006 2007
/* Program to printing of string*/
main( )
{
static char name[] = "Netcom";
int i=0;
while(i<=5)
{
printf("%c",name[i]);
i=i+1;
}
}
Another example of printing string as follows:
main( )
{
static char name[]="Netcom";
int i=0;
while(name[i]!='\0')
{
printf("%c",name[i]);
i=i+1;
}
}
Output of both the program is same, which is given below:
Netcom
Second program doesn't rely on the length of the string (no of characters in string) to print out its contents and hence is definitely more general than the earlier one.
Another example of printing a string is as follows:
main( )
{
char name[20];
printf("Enter institute name:");
scanf("%s",name);
printf("\n Institute Name is: %s !", name);
}
Output of the above program:
Enter institute name Netcom
Institute Name is: Netcom!
Note: The %s is a format specification for printing out a string.
While entering the string using scanf( ) we must be continuous about two things:
(a) The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn't perform bounds checking on character arrays. Hence, if you carelessly exceed the bounds there is always a danger of overwriting something important, and in that event you would have nobody to blame but yourselves.
(b) scanf( ) is not capable of receiving multi-word strings. Therefore names such as Netcom Computer would not be acceptable. To get past this limitation the function gets( ) can be used.
Declaration and initialization of string:
The string can also be initialized as follows:
char name[ ]="NETCOM";
The C compiler inserts the NULL('\0') character automatically at the end of the string. So initialization of NULL character is not essential. Character arrays can be initialized as follows:
char name[6]={'N', 'E', 'T', 'C', 'O', 'M'};
char name[7]={'N', 'E', 'T', 'C', 'O', 'M'};
In case (a) the output will not be 'NETCOM' but it contains some garbage value. In case (b) the output will be 'NETCOM' because it contains the size of array 7.
String and related standard functions
Every 'C' compiler supports a large number of strings handling library functions.
Table: Standard C string Library Functions
S.No.
|
Functions
|
Description
|
1.
|
strlen()
|
Determines length of a string.
|
2.
|
strcpy()
|
Copies a string from source to destination
|
3.
|
strncpy()
|
Copies characters of a string to another string upto the specified length.
|
4.
|
strcmp()
|
Compares characters of two strings
|
5.
|
stricmp()
|
Compares two strings.
|
6.
|
strncmp()
|
Compares characters of two strings upto the specified length.
|
7.
|
strnicmp()
|
Compares characters of two strings upto the specified length. Ignores case.
|
8.
|
strlwr()
|
Converts uppercase characters of a string to lowercase.
|
9.
|
strupr()
|
Converts lowercase characters of a string to uppercase.
|
10.
|
strdup()
|
Duplicates a string.
|
11.
|
strchr()
|
Determines first occurrence of a given character in a string.
|
12.
|
strrchr()
|
Determines last occurrence of a given character in a string.
|
13.
|
strstr()
|
Determines first occurrence of a given string in another string.
|
14.
|
strcat()
|
Appends source string to destination string.
|
15.
|
strncat()
|
Appends source string to destination string upto specified length.
|
16.
|
strrev()
|
Reverses all characters of string.
|
17.
|
strset()
|
Sets all characters of string with a given argument or symbol.
|
18.
|
strnset()
|
Sets specified numbers of characters of string with a given argument or symbol.
|
19.
|
strspn()
|
Finds upto what length two strings are identical.
|
20
|
strpbrk()
|
Searches the first occurrence of the character in a given string and then it displays the string starting from that character.
|