Our website is made possible by displaying online advertisements to our visitors.Please consider supporting us by disabling your ad blocker.
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Translate it in your own Language

Thursday, May 1, 2014

String in C

  • C Strings are nothing but array of characters ended with 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 for C string: 

  • char string[20] = { ‘d’ , ’i’ , ‘v’ , ‘a’ , ‘s’ , ‘n’ , ‘i’ , ‘k’ , ‘h’ , ’r’ , ‘a’  , ‘\0’}; (or)
  • char string[20] = “divasnikhra”; (or)
  • char string []    = “divasnikhra”;
  • Difference between above declarations are, when we declare char as “string[20]“, 20 bytes of memory space is allocated for holding the string value.
  • When we declare char as “string[]“, memory space will be allocated as per the requirement during execution of the program.

      Example program for C string:

      #include <stdio.h>
      #include<conio.h>
      void main ()
      {
         char string[20] = "divasnikhra";
         printf("The string is :  %s \n", string );
         getch();
      }

      Output:
      The string is :     divasnikhra


      C String functions:
      string.h header file supports all the string functions in C language. All the string functions are given below. Click on each function name to display an example program.

      strcat(str1, str2)  Concatenates str2 at the end of str1.

      strcpy(str1, str2)  Copies str2 into str1

      strlen(strl)  gives the length of str1.

      strcmp(str1, str2)  Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 > str2.

      strchr(str1, char)  Returns pointer to first occurrence of char in str1.

      strstr(str1, str2)  Returns pointer to first occurrence of str2 in str1.

      strcmpi(str1,str2)  Same as strcmp() function. But, this function negotiates case.  “A” and “a” are treated as same.

      strdup()  duplicates the string

      strlwr()  converts string to lowercase

      10 strncat()  appends a portion of string to another

      11 strncpy()  copies given number of characters of one string to another

      12 strrchr()  last occurrence of given character in a string is found

      13 strrev()  reverses the given string

      14 strset()  sets all character in a string to given character

      15 strupr()  converts string to uppercase

      16 strtok()  tokenizing given string using delimiter


      Example program for strlen:
      It counts number of characters available in a string.

      #include <stdio.h>
      #include <string.h>
      #include<conio.h>
      void main( )
      {
          int len1, len2 ;
          char array1[50]="virtualctutorials.blogspot.com" ;
          char array2[50]="Divasnikhra " \
                          "Avantika";
          len1 = strlen(array1) ;
          len2 = strlen(array2) ;
          printf ( "\nFirst string length  = %d \n" , len1 ) ;
          printf ( "\nSecond string length  = %d \n" , len2 ) ;
          getch();
      }

      Output:
      First string length = 30
      Second string length = 19


      Example program for strcpy:
      It copies contents of one string to another

      #include <stdio.h>
      #include <string.h>
      #include<conio.h>
      void main( )
      {
         char source[ ] = "virtualctutorials.blogspot.com" ;
         char target[50] ;
         strcpy ( target, source ) ;
         printf ( "\nContetnt of source string = %s", source ) ;
         printf ( "\nContetnt of target string = %s", target ) ;
         getch();
      }

      Output:
      Contetnt of source string = virtualctutorials.blogspot.com
      Contetnt of target string = virtualctutorials.blogspot.com


      Example program for strcat:
      It concatenates two given strings.

      #include <stdio.h>
      #include <string.h>
      #include<conio.h>
      void main( )
      {
         char source[ ] = " Divas" ;
         char target[20]= " C Tutorial" ;
         strcat ( target, source ) ;
         printf ( "\nContent of source string = %s", source ) ;
         printf ( "\nContetnt of target string = %s", target ) ;
         getch();
      }

      Output:
      Contetnt of source string = Divas
      Contetnt of target string = C Tutorial Divas


      Example program for strcmp:
      It compares two strings and returns 0 if they are same. If string1< string2, it returns < 0 value. If string1> string2, it returns > 0 value.

      #include <stdio.h>
      #include <string.h>
      #include<conio.h>
      void main( )
      {
         char str1[ ] = "fresh" ;
         char str2[ ] = "refresh" ;
         int i, j, k ;
         i = strcmp ( str1, "fresh" ) ;
         j = strcmp ( str1, str2 ) ;
         k = strcmp ( str1, "f" ) ;
         printf ( "\n%d %d %d", i, j, k ) ;
         getch();
      }

      Output:
      0 -1 1


      Example program for strchr:
      It returns pointer to the first occurrence of character in given string.

      #include <stdio.h>
      #include <string.h>
      #include <conio.h>
      void main ()
      {
         char string[55] ="This is a string for testing";
         char *p;
         p = strchr (string,'i');
         while (p!=NULL)
         {
            printf ("Character i found at %d\n",p-string+1);
            p=strchr(p+1,'i');
         }
         getch();
      }

      Output:
      Character i found at 3Character i found at 6Character i found at 14Character i found at 26


      Example program for strstr:
      It returns pointer to the first occurrence of string 2 in given string1.

      #include <stdio.h>
      #include <string.h>
      #include <conio.h>
      void main ()
      {
         char string[55] ="This is a string for testing";
         char *p;
         p = strstr (string,"for");
         if (p)
         {
            printf("string found\n" );
         }
         else
            printf("string not found");
         getch();
      }

      Output:
      string found


      Example program for strrchr:
      It finds the last occurrence of given character in a string.

      #include <stdio.h>
      #include <string.h>
      #include <conio.h>
      void main ()
      {
        char string[50] = "This is a string for testing";
        char *p;
        p = strrchr(string,'t');
        printf ("The position of last occurrence of 't' is found " \
                "at %d \n",p-string+1);
        getch();
      }

      Output:
      The position of last occurrence of ‘t’ is found at 25


      Example program for strset:
      It sets all the characters in a string to given character.

      #include<stdio.h>
      #include<string.h>
      #include<conio.h>
      void main()
      {
         char str[20] = "Test String";
         printf("Original string is : %s", str);
         printf("Test string after string set : %s",strset(str,'#'));
         printf("After string set: %s",str);
         getch();
      }

      Output:
      Original string is :      Test String
      Test string after string set : ###########


      Example program for strnset:
      It sets the portion of characters in a string to given character.

      #include<stdio.h>
      #include<string.h>
      #include<conio.h>
      void main()
      {
          char str[20] = "Test String";
          printf("Original string is : %s", str);
          printf("Test string after string n set" \
                 " : %s", strnset(str,'#',4));
          printf("After string n set : %s", str);
          getch();
      }

      Output:
      Original string is :      Test String
      Test string after string set : #### String


      Example program for strupr:
      It converts string to uppercase.

      #include<stdio.h>
      #include<string.h>
      #include<conio.h>
      void main()
      {
          char str[] = "Modify This String To Upper";
          printf("%s\n",strupr(str));
          getch();
      }

      Output:
      MODIFY THIS STRING TO UPPER


      Example program for strlwr:
      It converts string to lowercase.

      #include<stdio.h>
      #include<string.h>
      #include<conio.h>
      void main()
      {
          char str[] = "MODIFY This String To LOwer";
          printf("%s\n",strlwr (str));
          getch();
      }

      Output:
      modify this string to lower


      Example program for strncat:
      It appends a portion of string to another string.

      #include <stdio.h>
      #include<conio.h>
      #include <string.h>
      void main ()
      {
         char s1[30];
         char s2[20];
         strcpy (s1,"String1 ");
         strcpy (s2,"String2 ");
         strncat (s1, s2, 3);
         printf("Result : %s", s1);
         getch();
      }

      Output:
      Result : String1 Str


      Example program for strcpy and strncpy:
      It copies given number of characters of one string to another.

      #include <stdio.h>
      #include<conio.h>
      #include <string.h>
      void main ()
      {
        char s1[30];
        char s2[20];
        strcpy (s2,"String2 ");
        strncpy (s1, s2, 4);
        printf("Result : %s", s1);
        getch();
      }

      Output:
      Result : Stri


      Example program for strtok:
      It tokenizes the given string using delimiter. That means, It parses a given string using delimiter.

      #include <stdio.h>
      #include<conio.h>
      #include <string.h>
      void main ()
      {
        char string[50] ="Test,string1,Test,string2:Test:string3";
        char *p;
        printf ("string  \"%s\" is split into tokens:\n",string);
        p = strtok (string,",:");
        while (p!= NULL)
        {
          printf ("%s\n",p);
          p = strtok (NULL, ",:");
        }
        getch();
      }

      Output:
      string “Test,string1,Test,string2:Test:string3″ is split into tokens:Teststring1Teststring2Teststring3


      Example program for strdup:
      It duplicates the given string.

      #include <stdio.h>
      #include<conio.h>
      #include <string.h>
      void main()
      {
          char *p1 = "Raja";
          char *p2;
          p2 = strdup(p1);
          printf("Duplicated string is : %s", p2);
          getch();
      }

      Output:
      Duplicated string is : Raja


      Example program for strrev:
      It reverses the given string.

      #include<stdio.h>
      #include<conio.h>
      #include<string.h>
      void main()
      {
         char name[30] = "string";
         printf("Enter your name to reverse\n");
         strrev(name);
         printf("Reversed name is : \n%s\n",name);
         getch();
      }

       Output:

      gnirts

      No comments:

      Post a Comment

      More Details About CrackMNC

      Follow Our Google+ Page

      Copyright @ CrackMNC 2014-2024
      Divas Nikhra Theme by Crack MNC