<http://lib.cnfolio.com/ENG421TextStrings>
Introduction to Algorithms and Programming

Using text strings



Strings are arrays of characters


A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

char message[ 33 ] = { '\0' };

message[0] message[1] message[2] ( 3 to 31 ) message[32]
\0 \0 \0 \0 \0




When using string related functions, remember to add the header file for the standard string library functions.

#include <string.h>




Length of a text string


Use the strlen() function to find the length of a text string. Read the detailed reference description.

#include <stdio.h>
#include <string.h>

int main( void )
{
   char word1[] = "Welcome";
   char word2[ 30 ] = { '\0' };
   char word3[ 15 ] = { 't', 'o', ' ', 'E', 'N', 'G', '4', '2', '1' };

   printf( "%d \n", strlen( word1 ) );
   printf( "%d \n", strlen( word2 ) );
   printf( "%d \n", strlen( word3 ) );
   printf( "%d \n", strlen( "computers" ) );
}




Comparison of 2 strings


Use the strncmp() function to compare 2 strings for equality. Read the detailed reference description.

#include <stdio.h>
#include <string.h>

int main( void )
{
   char word1[] = "Welcome";
   char word2[ 30 ] = { '\0' };
   char word3[ 15 ] = { 't', 'o', ' ', 'E', 'N', 'G', '4', '2', '1' };

   printf( "%d \n", strncmp( word1, word2, 6 ) );
   printf( "%d \n", strncmp( word1, word3, 6 ) );
   printf( "%d \n", strncmp( word1, "Welcome", 6 ) );
   printf( "%d \n", strncmp( word1, "welcome", 6 ) );
   printf( "%d \n", strncmp( word1, "Welcome back", 8 ) );
   printf( "%d \n", strncmp( word1, "Welcome back", 4 ) );
}




Saving output to a string


Use the sprintf() function to store formatted output into a text string. Read the detailed reference description.

#include <stdio.h>
#include <string.h>

int main( void )
{
   char word1[] = "Welcome";
   char word2[ 30 ] = { '\0' };
   char word3[ 15 ] = { 't', 'o', ' ', 'E', 'N', 'G', '4', '2', '1' };

   sprintf( word2, "%s %s", word1, word3 );
   printf( "%s \n", word2 );
   sprintf( word2, "%d + %d = %d", 2, 8, 10 );
   printf( "%s \n", word2 );
}




Copying a string


Use the strcpy() function to copy one string to another string. Read the detailed reference description.

#include <stdio.h>
#include <string.h>

int main( void )
{
   char word1[] = "Welcome";
   char word2[ 30 ] = { '\0' };
   char word3[ 15 ] = { 't', 'o', ' ', 'E', 'N', 'G', '4', '2', '1' };

   strcpy( word2, word1 );
   printf( "%s \n", word2 );
   strcpy( word2, word3 );
   printf( "%s \n", word2 );
   strcpy( word2, "computing lab" );
   printf( "%s \n", word2 );
}




Appending to a string


Use the strcat() function to append one string to the end of another string. Read the detailed reference description.

#include <stdio.h>
#include <string.h>

int main( void )
{
   char word1[] = "Welcome";
   char word2[ 30 ] = { '\0' };
   char word3[ 15 ] = { 't', 'o', ' ', 'E', 'N', 'G', '4', '2', '1' };

   strcat( word2, word1 );
   printf( "%s \n", word2 );
   strcat( word2, word3 );
   printf( "%s \n", word2 );
   strcat( word2, " computing lab" );
   printf( "%s \n", word2 );
}




Parsing a string


Text in a string can be parsed into smaller units using the strtok() function. Read the detailed reference description.

#include <stdio.h>
#include <string.h>

int main( void )
{
  char originalText[ 128 ] = "rrrr gggg bbbb wwww";
  char copyOfText[ 128 ] = { '\0' };
  char delimiters[ 4 ] = " "; /* A space character separates each token in the originalText variable */

  /*
  There is no need for a full array of characters
  when using tokens from parsing.
  */

  char *nextToken = NULL;

  /*
  Make copy of original string because the strtok() function
  makes changes to the string when it performs the parsing.
  */

  strcpy( copyOfText, originalText );

  /* Function call to start the parsing */
  nextToken = strtok( copyOfText, delimiters );
  while ( nextToken != NULL )
  {
    printf( "%s \n", nextToken );

    /*
     This function call to continue parsing is different
     than the function call to start the parsing.
     */

    nextToken = strtok( NULL, delimiters );
  }
}