Strings - C Programming

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 function gets().The usage of functions gets() and its counter part puts() 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 string
  • strlwr - Converts a string to lowercase
  • strupr - Converts a string to uppercase
  • strcat - Appends one string at the end of another
  • strncat - Appends first n characters of a string at the end of
    another
  • strcpy - Copies a string into another
  • strncpy - Copies first n characters of one string into another
  • strcmp - Compares two strings
  • strncmp - Compares first n characters of two strings
  • strcmpi - 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 case
  • strdup - Duplicates a string
  • strchr - Finds first occurrence ofa given character in a string
  • strrchr - Finds last occurrence ofa given character in a string
  • strstr - Finds first occurrence of a given string in another string
  • strset - Sets all characters ofstring to a given character
  • strnset - Sets first n characters ofa string to a given character
  • strrev - 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

Example Statement for String in C Language
1. Check whether two strings are anagrams or not
2. Find a frequenct of character in string
3. Insert substring into a string
4. Substring from a position to length
5. Check if a string is a subsequence of another string
6. Delete vowels from a string

Next - Functions


You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.