String

Introduction to string

In C programming, a string is a sequence of characters terminated with a null character. String is an one dimentional character array. Strings are one-dimensional array of type characters terminated by a null character '\0'. This null character indicates the end of the string. Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.

Example
char c[  ]="c string";

It is represent in string as follows

string
Declaration
char a[5];

Here, we declared a string of 5 characters.

string

String functions and its purposes:

1.strlen(str);

Returns the length of string str.

Example
              // c program 
              // example of strlen() function.
              
                       
                  #include<stdio.h> 
                  #include<string.h> 
              
                  
              int main() 
              { 
                 char ch[]={'h', 'e', 'l', 'l', 'o','\0'}; 
              
                 printf("Length of string is: %d", strlen(ch)); 
              
                 return 0; 
              }
          

Output-

              Length of string is: 5
          
2.strcpy(str1,str2);

Copies string str2 into str1.

Example
                
          // C program 
          // example of strcpy() function.
             
                      
                 #include<stdio.h> 
                 #include<string.h>
             
                 
                   int main ()
                   {
                       char str1[]="Hello Guys!";
                       char str2[] = "C programming";
                      
                       strcpy(str2, str1);
                       printf ("str1: %s\nstr2: %s, str1, str2,);
                       return 0;
                   }
                
         

Output-

          str1: Hello Guys!
          str2: Hello Guys!
         
3.strcmp(Str1, Str2);

Returns 0 if Str1 and Str2 are the same; less than 0 if Str1 < Str2; greater than 0 if Str1>Str2.

Example
           // C program 
           // example of strcmp() function. 
             
                      
                 #include<stdio.h> 
                 #include<string.h>
             
                 
             int main() 
               { 
               	
               	char Str1[] = "C programming"; 
               	char Str2[] = "C programming"; 
               	
               	// Using strcmp() 
               	int k = strcmp(Str1, Str2); 
               	
               	if (k==0) 
               		printf("Strings are equal"); 
               	else
               		printf("Strings are unequal"); 
               	
               	printf("\nValue returned by strcmp() is: %d" , k); 
               	return 0; 
               }
         

Output-

           Strings are equal
           Value returned by strcmp() is:  0
         
4.strcat(str1, str2);

Concatenates string str2 onto the end of string str1.

Example
           // C program 
           // example of strcat() function. 
             
                      
                 #include<stdio.h> 
                 #include<string.h>
             
                 
             int main()
             {
             
                // Take any two strings
                char str1[50] = "efghi";
                char str2[50]= "abcd";
             
                strncat(str2, str1);
                                           
                // Prints the string
                printf("Source string : %s\n", str1);
                printf("Destination string : %s", str2);
                 
                return 0;
             }
         

Output-

           Source string : efghi
           Destination string : abcdefghi  
         
5.strrev(str1);

Reverse the string str1.

Example
            // C program 
            // example of strrev() function. 
              
                       
                  #include<stdio.h> 
                  #include<string.h> 
              
                  
              
              int main() 
              { 
                 char str1[50] = "hello"; 
                 
                 printf("The given string is =%s\n",str1); 
                 
                 printf("After reversing string is =%s",strrev(str1)); 
                 
                 return 0; 
              }
          

Output-

            The given string is =hello
            After reversing string is =olleh
          
6.strchr(str1, ch);

Returns a pointer to the first occurrence of character ch in string str1.

Example
           // C program 
           // example of strchr() function. 
             
                      
                 #include<stdio.h> 
                 #include<string.h> 
             
                 
            
                int main () {
                   char str1[] = "programming"; 
                   char ch = 'g'; 
                   char *p;
                   p = strchr(str1, ch);
                   printf("String starting from %c is: %s", ch, p);
                   return 0;
                }
         

Output-

            String starting from g is: gramming
         
7.strstr(str1, str2);

Returns a pointer to the first occurrence of string str2 in string str1.

Example
       // C program 
       // example of strstr() function. 
         
                  
             #include<stdio.h> 
             #include<string.h> 
         
             
        
         int main() 
         { 
             // Take any two strings 
             char str1[] = "learnCprogramming"; 
             char str2[] = "Cpro"; 
             char* p; 
         
             // Find first occurrence of str2 in str1 
             p = strstr(str1, str2); 
         
             // Prints the result 
             if (p) { 
                 printf("String found\n"); 
                 printf("First occurrence of string '%s' in '%s' is '%s'", str2, str1, p); 
             } else
                 printf("String not found\n"); 
         
             return 0; 
         }
     

Output-

          String found
          First occurrence of string 'Cpro' in 'learnCprogramming' is 'Cprogramming'
      
Following example uses some of the above string functions
       
                
           #include<stdio.h> 
           #include<conio.h> 
       
           
           int main () {
           
              char s1[12] = "Hello";
              char s2[12] = "World";
              char s3[12];
              int  len ;
           
              /* copy s1 into s3 */
              strcpy(s3, s1);
              printf("strcpy( s3, s1) :  %s\n", s3 );
           
              /* concatenates s2 and s3 */
              strcat( s1, s2);
              printf("strcat( s1, s2):   %s\n", s1 );
           
              /* total lenghth of s1 after concatenation */
              len = strlen(s1);
              printf("strlen(s1) :  %d\n", len );
           
              return 0;
           }
   

Output-

                    strcpy( s3, s1) :  Hello
                    strcat( s1, s2):   HelloWorld
                    strlen(s1) :  10
                
Functions used to read and write string
Examples For Practice
  1. Implement C program which reads and displays your name on screen.
  2. Implement C program to swap two strings.
  3. Implement C program to find the length of the string.
  4. Implement C program to calculate the blank spaces in string.
  5. Implement C program for deciding whether given string is palindrome or not. (Use string handling functions)