String-in c .

In this session i have discussed string in C .


String is nothing but it is a array of character with null character(0\) termination . The string is always enclosed with double quote("Ram") . The character always enclosed with single quote('r') .


Example : Write a program that demonstrate the string .

    #includ<stdio.h> 
    int main()
    {  
          char string1[10]={'h','e','l','l','o','\0'};
          char string2[10]={"hello"};
          char string3[]={"hello"};
          printf("string1 : %s\n",string1);
          printf("string2 : %s\n",string2);
          printf("string3 : %s\n",string3);
          return 0;
    }
    
    OUTPUT : string1 : hello
             string2 : hello
             string3 : hello
    

String functions in c described as follows

1-strlen()

The strlen() function is used to return the legth of string .

Example : Write a C program that demonstrate the strlen function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    {  
          char string[10]={"hello"};
          int len=strlen(string);
          printf("string length : %d",len);
          return 0;
    }
    
    OUTPUT : string length is : 5
    

2-strcmp()

The strcmp() function is used to compare the two string if both string are same then it will return 0 other wise return negative or positive value based on comparison .

Example : Write a C program that demonstrate the strcmp function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
         char s1[20] = "hello";
         char s2[20] = "hello";
         if (strcmp(s1, s2) ==0)
         {
                printf("s1  and s2 are equal");
         }
         else
         {
                printf("s1 and s2 are different");
         }
          return 0;
    }
    
    OUTPUT : s1 and s2 are equal
    

2-strncmp()

The strncmp() function is used to compare the two string till n character . In other words the first string compare with second string till n character . If both are same then it will return 0 .

Example : Write a C program that demonstrate the strncmp function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
         char s1[20] = "hello";
         char s2[20] = "hello World";
        /* below it is comparing first 5 characters of s1 and s2*/
        if (strncmp(s1, s2, 5) ==0)
        {
             printf("s1 and s2 are equal");
        }
        else
        {
             printf("s1 and s2 are different");
        }
          return 0;
    }
    
    OUTPUT : s1 and s2 are equal
    

3-strcmpi()

The strcmpi() function is used to compare the two string and ignore the case if both string are same then it will return 0 other wise return negative or positive value based on comparison .

Example : Write a C program that demonstrate the strcmpi function .

    #includ<stdio.h>
    #include<string.h> 
    int main()
    { 
         char s1[20] = "hello";
         char s2[20] = "HELLO";
         if (strcmpi(s1, s2) ==0)
         {
                printf("s1  and s2 are equal ignored case");
         }
         else
         {
                printf("s1 and s2 are different");
         }
          return 0;
    }
    
    OUTPUT : s1 and s2 are equal ignored case
    

4-strcat()

It concatenates two strings and returns the concatenated string in first string.

Example : Write a C program that demonstrate the strcat function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
         char s1[20] = "hello";
         char s2[20] = "world";
         strcat(s1,s2);
         printf("Concatnates string : %s",s1);
         return 0;
   }
    
    OUTPUT : helloworld
    

5-strncat()

It concatenates two strings till n characters and returns the concatenated string in first string.

Example : Write a C program that demonstrate the strncat function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
         char s1[20] = "hello";
         char s2[20] = "world";
         strncat(s1,s2,4);
         printf("Concatnates string : %s",s1);
         return 0;
   }
    
    OUTPUT : helloworl
    

6-strcpy()

The strcpy() is used to copy the one string into another string . That means second string copied into first string .

Example : Write a C program that demonstrate the strcpy function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
         char s1[30] = "hello";
         char s2[30] = "I am copied into s1";
        /* this function has copied s2 into s1*/
        strcpy(s1,s2);
        printf("%s", s1);
        return 0;
   }
    
    OUTPUT : I am copied into s1
    

7-strncpy()

The strncpy() is used to copy the one string into another string . That means second string copied into first string .

If string2>n it will copied the specified number of character into first string .

If string2<n it will copied the specified number of character into first string and append some terminator to fullfill the n character .

Example : Write a C program that demonstrate the strncpy function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
         char s1[30] = "hello";
         char s2[30] = "I am copied into s1";
        /* this function has copied s2 into s1*/
        strncpy(s1,s2,12);
        printf("%s", s1);
        return 0;
   }
    
    OUTPUT : I am copied
    

8-strchr()

The strchr() is used to search the string that you specified the character .

Example : Write a C program that demonstrate the strchr function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char mystr[20] = "I am an example abc";
            printf ("%s", strchr(mystr, 'e'));
        return 0;
   }
    
    OUTPUT : example abc
    

9-strrchr()

The strrchr() is very similar to strchr() . It is used to search the string that you specified the character in reverse order .

Example : Write a C program that demonstrate the strrchr function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char mystr[20] = "I am an example abc";
            printf ("%s", strchr(mystr, 'a'));
        return 0;
   }
    
    OUTPUT : abc
    

10-strrev()

The strrev() is used to reverse the specified string .

Example : Write a C program that demonstrate the strrev function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char mystr[20] = "ram";
            printf ("%s", strrev(mystr));
            return 0;
   }
    
    OUTPUT : mar
    

11-strupr()

The strupr() is used to convert the specified string into upper case .

Example : Write a C program that demonstrate the strupr function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char mystr[20] = "ram";
            printf ("%s", strupr(mystr));
            return 0;
    }
    
    OUTPUT : RAM
    

12-strlwr()

The strupr() is used to convert the specified string into lower case .

Example : Write a C program that demonstrate the strlwr function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char mystr[20] = "RAM";
            printf ("%s", strlwr(mystr));
            return 0;
    }
    
    OUTPUT : ram
    

13-gets()

The gets() function enables the user to enter some characters followed by the enter key . All the character stored in a character array .

Example : Write a C program that demonstrate the strlwr function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char s1[20];
            printf("Enter the string \n");
            gets(s1);
            printf("%s",s1);
            return 0;
    }
    
    OUTPUT : Enter the string 
             Ram is a good boy
             Ram is a good boy
    

14-puts()

The puts() function is very similar to printf() function . The puts() is used to print the string on console which is previously read by the gets() or scanf() .

Example : Write a C program that demonstrate the puts function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char s1[20];
            printf("Enter the string \n");
            gets(s1);
            puts(s1);
            return 0;
    }
    
    OUTPUT : Enter the string 
             Ram is a good boy
             Ram is a good boy
    

15-getchar()

The getchar() function is used to get/read the character from keyboard .

Example : Write a C program that demonstrate the getchar function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char ch;
            printf("Enter the character\n");
            ch=getchar();
            printf("%c",ch);
            return 0;
    }
    
    OUTPUT : Enter the character 
             a
             a
    

16-putchar()

The putchar() function is used to write the character on output screen .

Example : Write a C program that demonstrate the putchar function .

    #includ<stdio.h> 
    #include<string.h>
    int main()
    { 
            char ch;
            printf("Enter the character");
            ch=getchar();
            putchar(ch);
            return 0;
    }
    
    OUTPUT : Enter the character 
             a
             a
    

Welcome to AndroidATPoint.

back image                     next image