Declaring the string functions in C's standard library

  • The declarations of the string functions in C's standard library are contained in header files:

        #include <string.h>  

  • How to get the manual page on the string functions:

        man string

     

     

     

Samples of string functions

  • int strlen(char *s):  

    • returns the length (not including the sentinel '\0') of string s

  • char *strcpy(char *s1, char *s2):  

    • copy string s2 into the string s1
    • strcpy(s1, s2): returns s1

  • char *strcat(char *s1, char *s2):  

    • append (= catenate) string s2 after the string s1
    • (string s2 is unchanged)
    • strcat(s1, s2): returns s1

  • int strcmp(char *s1, char * s2):   compare the strings s1 and s2

      • returns a value < 0 if s1 is lexicographically smaller than s2
      • returns a value 0 if s1 is lexicographically equal to s2
      • returns a value > 0 if s1 is lexicographically larger than s2

I like to show you how some of these functions are implemented using pointer arithmetic....

The strlen( ) function

  • The strlen(s) function is as follows:

    int strlen(char *s)
    {
        int len = 0;  // Initialize len = 0
    
        while ( *s != '\0' )  // Not the sentinel
        {
            len++;     // One more character
    	s++;	   // Advance to next char
        }
    
       return(len);
    }

     

DEMO: demo/C/set2/strlen1.c

The abbreviated version strlen( ) function

  • The strlen(s) function can be abbreviated as follows:

    int strlen(char *s)
    {
        int len = 0;  // Initialize len = 0
    
        while ( *s++ != '\0' )  // Not the sentinel
        {
            len++;     // One more character
    
        }
    
       return(len);
    }

    because we increment s after using it.

DEMO: demo/C/set2/strlen2.c

Background info: the assignment expression

  • Many of the string functions make use of the fact that the assignment operator (=) returns the assigned value:

        x = 3;     // = returns 3 !!! 
    

  • Example:

    int main(int argc, char *argv[])
    {
       int x;
    
       printf(">>> %d\n", x=3 ); // Prints the return value 3
    }
    
    

DEMO: demo/C/set2/assign-op1.c

Background info: copying an array using pointer arithmetic

  • Suppose an array A contains non-zero elements and the value 0 indicate the end of the array:

        A[] =  1  2  3  0           B[] =  x  x  x  x
               ^                           ^
               |			       |
               p                           q
    

  • We can copy all elements in array A[] to the array B[] using this loop:
     

       int *p = A;
       int *q = B;
    
       while ( (*q++ = *p++) != 0 );   
    

    I will explain the loop next...

Background info: copying an array using pointer arithmetic

  • Suppose an array A contains non-zero elements and the value 0 indicate the end of the array:

        A[] =  1  2  3  0           B[] =  x  x  x  x
               ^                           ^
               |			       |
               p                           q
    

  • We first initialize 2 pointer variable pointing to the start of each array:
     

       int *p = A;
       int *q = B;
    
       while ( (*q++ = *p++) != 0 );   
    

    What does *q++ = *p++ do ?

Background info: copying an array using pointer arithmetic

  • Suppose an array A contains non-zero elements and the value 0 indicate the end of the array:

        A[] =  1  2  3  0           B[] =  1  x  x  x
                  ^                           ^
                  |			          |
                  p                           q
    

  • *q++ = *p++ will copy the current value from A to B (see diagram above)... and move forward !!!

       int *p = A;
       int *q = B;
    
       while ( (*q++ = *p++) != 0 );   
    

    *q++ = *p++ will return the value copied (= 1) and 1 != 0...

Background info: copying an array using pointer arithmetic

  • Suppose an array A contains non-zero elements and the value 0 indicate the end of the array:

        A[] =  1  2  3  0           B[] =  1  2  x  x
                     ^                           ^
                     |			     |
                     p                           q
    

  • *q++ = *p++ will copy the current value from A to B (see diagram above)... and move forward !!!

       int *p = A;
       int *q = B;
    
       while ( (*q++ = *p++) != 0 );   
    

    *q++ = *p++ will return the value copied (= 2) and 2 != 0...

Background info: copying an array using pointer arithmetic

  • Suppose an array A contains non-zero elements and the value 0 indicate the end of the array:

        A[] =  1  2  3  0           B[] =  1  2  3  x
                        ^                           ^
                        |			        |
                        p                           q
    

  • *q++ = *p++ will copy the current value from A to B (see diagram above)... and move forward !!!

       int *p = A;
       int *q = B;
    
       while ( (*q++ = *p++) != 0 );   
    

    *q++ = *p++ will return the value copied (= 3) and 3 != 0...

Background info: copying an array using pointer arithmetic

  • Suppose an array A contains non-zero elements and the value 0 indicate the end of the array:

        A[] =  1  2  3  0           B[] =  1  2  3  0
                           ^                           ^
                           |			   |
                           p                           q
    

  • *q++ = *p++ will copy the current value from A to B (see diagram above)... and move forward !!!

       int *p = A;
       int *q = B;
    
       while ( (*q++ = *p++) != 0 );   
    

    *q++ = *p++ will return the value copied (= 0) and 0 == 0:   while loop ends

The strcpy( ) function   using array notation

  • The strcpy(s1, s2) function written with array notation is:

       char *strcpy( char s1[], char s2[] )
       {
          int i = 0;
    
          /* ----------------------------------
             '\0' marks the end of the string !
    	 ---------------------------------- */
          while ( s2[i] != '\0' )   // Not end of s2
          {
             s1[i] = s2[i];   // Copy current character     
    	 i++;             // Move to next character
          }
    
          s1[i] = '\0';   // Mark the end of the s1 string    
    
          return(s1);
       }

DEMO: demo/C/set2/strcpy1.c

We can write a version of strcpy( ) using pointer arithmetic with the previously discussed technique

The strcpy( ) function   using pointer arithmetic

  • The strcpy(s1, s2) function written with pointer arithmetic is:

       char *strcpy( char s1[], char s2[] )
       {
          char *retVal = s1;  // Save s1 to return
    
          /* ----------------------------------
             '\0' marks the end of the string !
    	 ---------------------------------- */
          while ( (*s1++ = *s2++) != '\0' );   // Not end of s2
          
                // Copy current character     
    	    // and move to next character
                // until we copied '\0'
    
          // Note: '\0' has been copied ! 
    
          return(retVal);
       }

DEMO: demo/C/set2/strcpy3.c

Advice:   do not go too crazy with pointer arithmetic... the code is hard to read...

The strcmp( ) function   using array notation

  • The strcmp(s) function written with array notation is:

       int strcmp( char s1[], char s2[] )
       {
          int i = 0;
        
          /* ------------------------------------------------------
       	 Find the first distinct character in s1 and s2
       	 ------------------------------------------------------- */
          while ( s2[i] != '\0' && s1[i] != '\0' )
          {
       	 if ( s1[i] != s2[i] ) // s1 and s2 differ in char i
       	    break;             // exit while loop !
        
       	 i++;         // Otherwise, check next char
          }
    
          return s1[i] − s2[i];  // Returns < 0 if s1[i] < s2[i]
                                 // Returns   0 if s1[i] = s2[i]
    			     // Returns > 0 if s1[i] > s2[i]
       }

DEMO: demo/C/set2/strcmp1.c

The strcmp( ) function   using pointer arithmetic

  • The strcmp(s) function written with pointer arithmetic is:

       int strcmp( char s1[], char s2[] )
       {
          int i = 0;
        
          /* ------------------------------------------------------
       	 Find the first distinct character in s1 and s2
       	 ------------------------------------------------------- */
          while ( *s2   != '\0' && *s1   != '\0' )
          {
       	 if ( *s1   != *s2   ) // s1 and s2 differ in char i
       	    break;             // exit while loop !
        
       	 s1++; s2++;  // Otherwise, check next char
          }
    
          return *s1   − *s2  ;  // Returns < 0 if *s1   < *s2  
                                 // Returns   0 if *s1   = *s2  
    			     // Returns > 0 if *s1   > *s2  
       }

Instead of using a break, we can add (*s1 != *s2) == FALSE to the while-condition to exit the while-loop

The strcmp( ) function   using pointer arithmetic

  • Final version of strcmp( ):

       int strcmp( char s1[], char s2[] )
       {
          int i = 0;
        
          /* ------------------------------------------------------
       	 Find the first distinct character in s1 and s2
       	 ------------------------------------------------------- */
          while ( *s2   != '\0' && *s1   != '\0' && *s1   == *s2   )
          {
       	
       	   
        
       	 s1++; s2++;  // Otherwise, check next char
          }
    
          return *s1   − *s2  ;  // Returns < 0 if *s1   < *s2  
                                 // Returns   0 if *s1   = *s2  
    			     // Returns > 0 if *s1   > *s2  
       }

DEMO: demo/C/set2/strcmp3.c