STRINGS
Strings are arrays of characters, i.e., they are characters arranged one after another in memory. To mark the end of the string, C uses the null character. Strings in C are enclosed within double quotes. The string is stored in memory as ASCII codes of the characters, that make up the string appended with 0 (ASCII value of null).Initializing strings:
Ex: char day[]={ ‘F’, ‘R’, ‘I’, ‘D’, ‘A’, ‘Y’ 0 };
‘C’ offers the modified way to initialize the string as follows: char day[] = “FRIDAY”;
LENGTH OF AN ARRAY:
/* EX-27: PGM TO ACCEPT A STRING AND FIND ITS LENGTH USING ARRAY */ |
#include<stdio.h> main() { char str[30]; int i,len=0; clrscr(); printf("\n Enter a string: "); gets(str); for(i=0; str[i]!='\0'; i++) len++; printf("\n Length of %s = %d",str,len); getch(); } |
REVERSE A STRING USING AN ARRAY:
/* EX-28: PGM TO ACCEPT A STRING AND REVERSE IT USING ARRAY */ |
#include<stdio.h> main() { char str[30]; int i,len=0; clrscr(); printf("\n Enter a string: "); gets(str); for(i=0;str[i]!='\0';i++) len++; while(len>=0) { printf("%c",str[len]); len--; } getch(); } |
STANDARD LIBRARY FUNCTIONS:
strcat()
This function concatenates two strings, i.e., it appends one string at the end of another. The function accepts two strings as parameters and stores the contents of the second string at the end of the first.
/* EX-29: PGM TO CONCATENATE THE TWO STRINGS USING STD LIBRARY FUNCTION */ |
#include<string.h> main() { char str1[30]="SUMITHRA "; char str2[]="THIYAGARAJAN"; clrscr(); printf("\n\n\t\tCONCATENATED STRING : "); strcat(str1,str2); puts(str1); getch(); } |
Output: SUMITHRA THIYAGARAJAN |
Note:
1. Whenever you use the strcat function, check to see whether the first string is large enough to hold the resultant concatenated string.
2. In the declaration of array, if the subscript is omitted, it is assumed to be of the size of the data with which the array is initialized.
strcmp()
The function strcmp() compares two strings. This function is useful while writing programs for constructing and searching strings as arranged in a dictionary. The function accepts two strings as parameters and returns an integer, whose value is:
Less than 0 if the first sting is less than the second
Equal to 0 if both are identical
Greater than 0 if the first string is greater than the second
The function strcmp() compares the two strings, character by character, to decide the greater one. Whenever two characters in the string differ, the string, which has the character with a higher ASCII value, is greater.
For example, consider the strings hello and Hello. The first character itself differs. The ASCII code for h is 104, while that for H is 72. Since the ASCII code of H is greater, the string hello is greater than the string Hello. Once a differing character is found, there is no need to compare the other characters of the strings.
/* EX-30: PGM TO COMPARE TWO STRINGS USING STD LIBRARY FUNCTION */ |
#include<string.h> main() { char str1[30],str2[30]; int result; clrscr(); printf("Enter any two strings: \n"); scanf("%s %s", str1,str2); result=strcmp(str1,str2); if(result<0) printf("str1 < str2"); else if(result==0) printf("str1 == str2"); else printf("str1 > str2"); getch(); } |
Note: The operators <, > and == cannot be used directly with strings.
stricmp() or strcmpi()
This function also compares two strings, but it ignores case (i.e., upper case or lower case). Try to do the same example, used above.
strcpy()
The function strcpy, copies one string to another, character by character including null character.
syn: strcpy(target, source);
/* EX-31: PGM TO COPYING STRING FROM ONE VAR TO ANOTHER VAR USING STD LIBRARY FUNCTION */ |
#include<string.h> main() { char s1[60],s2[30]; clrscr(); printf("\n Enter a string: "); gets(s1); strcpy(s2,s1); printf("\n String after copied = %s",s2); getch(); } |
strlen()
The function strlen returns an integer, which denotes the length of the string passed. The length of a string is the number of characters presents in it, excluding the terminating null character.
/* EX-32: PGM TO FIND THE LENGTH OF STRING USING STD LIBRARY FUNCTION */ |
#include<stdio.h> main() { int len; char str[40]; clrscr(); printf("\n Enter a string: "); gets(str); len=strlen(str); printf("\n Length of the string %s = %d", str,len); getch(); } |
strrev()
The function strrev(), reverse a given string and it stores the reversed string into the same variable.
/* EX-33: PGM TO REVERSE A STRING USING STD LIBRARY FUNCTION */ |
#include<string.h> main() { char str[30]; clrscr(); printf("\n Enter a string: "); gets(str); strrev(str); printf("\n After reverse, the string = %s", str); getch(); } |