Strings in C examples



Share



Learn strings declaration, indexing string elements, passing string to function, pointer to string, function returning pointer to string, arrary of strings, array of pointers to string with examples.

Online courses related to this article


Course description

Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs



Course description

Learn, test your C programming skills and earn certificate for software jobs



Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs



Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs

What are strings? Strings declaration

  • What are strings?
  • A character string is a series of one or more characters terminated by a null character. Here is an example of a string: "How are you doing!" The double quotation marks are not part of the string. They inform the compiler that they enclose a string, just like single quotation marks identify a character. Strings in C are always stored with a terminating null character. The presence of the null character means that the array must have at least one more cell than the number of characters to be stored.

  • String declarations?
  • 
    char cstr[] = {'A','B','C','D','E','\0'};
    char tstr[] = "ABCDE";
    char *pstr = "ABCDE";
    
    

    I have declared string in three ways. cstr is the way of declaring a string using characters. Generally you will not use this way while declaring strings in your project. But I have included this format here to highlight the fact that string has a null character as the last character.

    tstr is the string declaration using double quotes. This is the frequently used way of declaring string. pstr is the way of declaring string using pointer. This way of declaring string is also very frequently used.


    Accessing string elements and their addresses

  • Accessing string elements
  • Like arrays string indexing starts from 0 not from 1. The address of a string is nothing but the name of the string. Also the addresses of string elements can be obtained using ampersand which is the address of operator.

    Accessing string elements, addresses.
    
    #include "stdio.h"
    int main()
    {
      char cstr[] = {'A','B','C','D','E','\0'};
      printf("cstr addresses %x, %x and %x\n",cstr, &cstr[1], cstr+2);
      printf("cstr contents %c, %c and %c\n",cstr[0], cstr[1], cstr[2]);
      char tstr[] = "ABCDE";
      printf("tstr addresses %x, %x and %x\n",tstr, &tstr[1], tstr+2);
      printf("tstr contents %c, %c and %c\n",tstr[0], tstr[1], cstr[2]);  
      char *pstr = "ABCDE";
      printf("pstr addresses %x, %x and %x\n",pstr, &pstr[1], pstr+2);
      printf("pstr contents %c, %c and %c\n",pstr[0], pstr[1], pstr[2]);  
      return 0;
    }      
    
    
  • Program output:
  • cstr addresses 7e8f920c, 7e8f920d and 7e8f920e
    cstr contents A, B and C
    tstr addresses 7e8f920c, 7e8f920d and 7e8f920e
    tstr contents A, B and C
    pstr addresses 10660, 10661 and 10662
    pstr contents A, B and C
    

    In the program output notice that the value of the variable cstr is nothing but the address of the first character of string cstr which is 7e8f920c. &cstr[1] prints the address of the second element of string cstr. Also notice that we can get address of the 3rd element of string cstr using cstr+2 notation. One more thing I want to highlight here is that as a char takes 1 byte of space in memory the address of each successive string element keeps incrementing by 1 byte like 7e8f920c, 7e8f920d, 7e8f920e. You will see similar observation for strings tstr and pstr.

    Passing string to a function

  • Illustration example
  • In the example below I have passed two strings inStr and outStr to function passStrToFunction. The purpose of the program is to copy the content of string inStr to string outStr and the change of the content of string outStr should be reflected in the main function. I have allocated memory at outStr of size of string inStr using malloc library function.

    
    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    void passStrToFunction(char *inStr,char *outStr);
    int main()
    {
      char *inStr = "Hello";
      char *outStr = malloc(strlen(inStr));
      passStrToFunction(inStr, outStr);
      printf("outStr:%s\n", outStr);
      return 0;
    }
    void passStrToFunction(char *inStr, char *outStr)
    {
      int i;
      for(i = 0;inStr[i] != '\0';i++)
      {
        outStr[i] = inStr[i];
      }
      outStr[i] = '\0';
    }
    
    

    Output:

    $ gcc prog.c
    $ ./a.out
    outStr:Hello
    $
    

    As you can see in the above program output, the outStr string has content "Hello". As inStr and outStr are passed to function passStrToFunction as pointers, writing to string outStr is reflected in the main function as well.

    Function returning pointer to string

  • Illustration example
  • In the below program we have done the same thing as the previous function passStrToFunction but using a function retStrFromFunction which returns a pointer to string outStr

    .
    
    #include "stdio.h"
    #include "string.h"
    #include "stdlib.h"
    char *retStrFromFunction(char *inStr);
    int main()
    {
      char *inStr = "Hello";
      char *outStr;
      outStr = retStrFromFunction(inStr);
      printf("outStr:%s\n", outStr);
      return 0;
    }
    char *retStrFromFunction(char *inStr)
    {
      char *outStr;
      outStr = malloc(strlen(inStr));
      int i;
      for(i = 0;inStr[i] != '\0';i++)
      {
        outStr[i] = inStr[i];
      }
      outStr[i] = '\0';
      return outStr;
    }
    
    

    Output:

    $ gcc prog.c
    $ ./a.out
    outStr:Hello
    $
    

    To Read Full Article

    Subscribe courses or Pay Per Course & logged in


    Exercises, Solutions

    High
    Low
    High
    High
    Medium
    Medium
    Low
    Medium
    High

    Want to contribute a new article? Write and upload your article information .
    Share


     Articles

     C Programming

     Linux Software

     Embedded Systems

     Python Programming

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs



     Online certificate courses

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software