·  C – Syntax – Variables – C string


- Info:

  - This tutorial shows hot to work with strings in C.

  - C has no built in type for storing strings, instead strings are stored as zero terminate character arrays.

  - Functions for manipulating C-strings are listed at http://www.cplusplus.com/reference/clibrary/cstring/strncpy/


- Procedure:

  - Create C Console Application Test.c.



    #include <stdio.h>


    int main () {


      //HELPER VARIABLES.-------------------------------------------------------------------------------

      char  src   [20] = "SOURCE";

      char  dest  [20] = "DESTINSTION";

      char  string[]   = "Hello";

      char  day[20];

      int   year;

      int   length;

      char  *chAdr;



      char  str1[20];                                 //Size is part of the type.

      char  str2[  ] = {'H','i','\0'};                //Manualy terminate with 0 or '\0'.

      char  str3[  ] = "Hello";                       //Hello World\0    \0 is added in the end.

      char  str4[ 6] = "Hello";                       //Size must match. \0 is added in the end.



      sprintf(str1, "I am %d", 23);                   //string="Today is 1999"



      chAdr  = strchr (src ,'R');                     //  Address of first occurence of 'R'   or NULL.

      chAdr  = strstr (src ,"URC");                   //  Address of first occurence of "URC" or NULL.


      length = strlen (src);                          //6 Number of characters before \0.



      sscanf("Thursday 1999" , "%s %d       ", day, &year );

      sscanf("Thursday 1999" , "%s          ", day);  //Thursday Takes the first word, before space.

      sscanf("Thursday 1999" , "Th%s        ", day);  //ursday   Skips Th & takes rest till first space.

      sscanf("smece 00 1999" , "%*s %*d %d  ", &year);//1999     Skips words that start with %*.

      sscanf("Sunday;Monday;", "%[^;]       ", day);  //Sunday   Take all chars till first ';'.Leave ';'

      sscanf("Sunday;Monday;", "Su%[^;]     ", day);  //nday     Skip "Su",take chars till ';'

      sscanf("Sunday;Monday;", "%*[^;];%[^;]", day);  //Monday   Skip till ';'.Skip ';'.Take till ';'   

      //COMBINE STRINGS.--------------------------------------------------------------------------------

      strcpy (dest, src   );                          //SOURCE\0        Copy src over dest.

      strncpy(dest, src, 3);                          //SOUTINSITION\0    Copy 3 bytes from src to dest

      strcat (dest, src   );                          //DESTINATIONSOURCE\0 Add src to dest.



      printf("%s      \n", string); //Hello     Print characters until you hit \0.

      printf("%.2s    \n", string); //He        Print maximum of 2 characters.

      printf("%7s     \n", string); //  Hello   Right justify with spaces.

      printf("%7.2s   \n", string); //     He   Right justify with spaces. Print maximum 2 characters.

      printf("%-7s<   \n", string); //Hello  <  Left  justify with spaces.

      printf("%-7.2s< \n", string); //He     <  Left  justify with spaces. Print maximum 2  characters.


      return 0;