1. What is String?
Strings are arrays of characters. Each member of array contains one of characters in the string.
Example
#include<stdio.h>
main()
{
char name[20];
printf("Enter your name : ");
scanf("%s",name);
printf("Hello, %s , how are you ?\n",name);
}
Output Results:
Enter your name : Vineet
Hello, Vineet, how are you ?
If user enters "Vineet" then the first member of array will contain 'V' , second cell will contain 'i' and so on. C determines end of a string by a zero value character. We call this character as NULL
character and show it with \0
character. (It's only one character and its value is 0, however we show it with two characters to remember it is a character type, not an integer).
Equally we can make that string by assigning character values to each member.
name[0]='B';
name[1]='r';
name[2]='i';
name[3]='a';
name[4]='n';
name[5]='\0';
As we saw in above example placeholder for string variables is %s
. Also we will not use a &
sign for receiving string values.
2. Point to Note
While entering the string using scanf()
we must be cautious about
two things:
- 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.
scanf()
is not capable of receiving multi-word strings. Therefore names such as "Vineet Choudhary" would be unacceptable. The way to get around this limitation is by using the functiongets()
.The usage of functionsgets()
and its counter partputs()
is shown below.
#include<stdio.h>
main( )
{
char name[25] ;
printf ("Enter your full name ") ;
gets (name) ;
puts ("Hello!") ;
puts (name) ;
}
And here is the output...
Enter your name Vineet Choudhary
Hello!
Vineet Choudhary
The program and the output are self-explanatory except for the fact that, puts()
can display only one string at a time (hence the use of two puts()
in the program above). Also, on displaying a string, unlike printf()
, puts()
places the cursor on the next line. Though gets()
is capable of receiving only one string at a time, the plus point with gets()
is that it can receive a multi-word string.
If we are prepared to take the trouble we can make scanf()
accept multi-word strings by writing it in this manner:
char name[25] ;
printf ("Enter your full name ") ;
scanf ("%[^\n]s", name) ;
Though workable this is the best of the ways to call a function, you would agree.
3. Standard Library String Functions
With every C compiler a large set of useful string handling library functions are provided in string.h
file.
strlen
- Finds length of a stringstrlwr
- Converts a string to lowercasestrupr
- Converts a string to uppercasestrcat
- Appends one string at the end of anotherstrncat
- Appends first n characters of a string at the end of
anotherstrcpy
- Copies a string into anotherstrncpy
- Copies first n characters of one string into anotherstrcmp
- Compares two stringsstrncmp
- Compares first n characters of two stringsstrcmpi
- Compares two strings without regard to case ("i" denotes
that this function ignores case)stricmp
- Compares two strings without regard to case (identical to
strcmpi)strnicmp
- Compares first n characters of two strings without regard
to casestrdup
- Duplicates a stringstrchr
- Finds first occurrence ofa given character in a stringstrrchr
- Finds last occurrence ofa given character in a stringstrstr
- Finds first occurrence of a given string in another stringstrset
- Sets all characters ofstring to a given characterstrnset
- Sets first n characters ofa string to a given characterstrrev
- Reverses string
4. Example
The following example uses some of the above-mentioned functions −
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
When the above code is compiled and executed, it produces the following result −
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
5. Examples
Next - Functions