Jun 24, 2016

C Programming #73: string.h library

C Programming #73: string.h library

This article tries to explores the library function that is provided by string.h. Like any other library which is collection of common library function which is put together so that programmer can easily use without writing them again. Similarly for strings there are some of common things programmer would like to do with strings like

  1. Find the length of string - strlen
  2. Copy a string - strcpy
  3. Compare two string - strcmp
  4. Find a character in string - strchr
  5. Find sub-string in string - strstr
  6. Concatenate two string - strcat

This article tries to give a small peek into string.h. It by no means covers all the functions. Main idea is that programmer should get good amount of idea how a library should be written.


All the above function prototypes have been standardized and their prototype looks as follows

/*Return the length of the string s.*/
size_t strlen(const char *s);

/*Copy the string src to dest, 
returning a pointer to the start of dest.*/
char *strcpy(char *dest, const char *src);

/*Compare the strings s1 with s2.*/
int strcmp(const char *s1, const char *s2);

/*Return a pointer to the first occurrence 
of the character c in the string s.*/
char *strchr(const char *s, int c);

/*Find the first occurrence of the  sub-string  
needle  in  the  string  haystack, returning a 
pointer to the found sub-string. */
char *strstr(const char *haystack, const char *needle);

/*Append the string src to the string dest, 
returning a pointer dest*/
char *strcat(char *dest, const char *src);

Already comments above the proto are explaining the library function. Some of small observation are

  1. size_t - It is new type which can store length. (Mostly typedef of unsigned int)
  2. All string that a function modifies are char*
  3. All string that a function won't modify are const char* - This is prevent library implement unknowingly modifying it.

Following sample program would demo the usage of above library function.

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

int main()
{
   char a[50] = "hello world";
   char b[50] = ""; /*Empty string */
   char c[50] = "india";
   int d;
   char *p;

   printf("Length of string a is %d\n", strlen(a));

   printf("String b before copy is %s\n", b);
   strcpy(b, a);
   printf("String b after copy is %s\n", b);

   if(0 == (d = strcmp(a, b))) {
       printf("string %s and %s are same\n", a, b);
   }else {
       printf("string %s and %s are not same\n", a, b);
   }
   printf("d after strcmp is %d\n", d);

   if(0 == (d = strcmp(a, c))) {
       printf("string %s and %s are same\n", a, c);
   }else {
       printf("string %s and %s are not same\n", a, c);
   }
   printf("d after strcmp is %d\n", d);

   if(0 == (d = strcmp(c, a))) {
       printf("string %s and %s are same\n", a, c);
   }else {
       printf("string %s and %s are not same\n", a, c);
   }
   printf("d after strcmp is %d\n", d);

   if(NULL != (p = strchr(a , 'o'))) {
       printf("o found in string %s and return is %s\n", a, p);
   }
   if(NULL != (p = strstr(a , "world"))) {
       printf("w found in string %s and return is %s\n", a, p);
   }
   strcat(c, " says,");
   strcat(c, a);
   printf("c after strcat is %s\n", c);

   return 0;
}
Length of string a is 11
String b before copy is 
String b after copy is hello world
string hello world and hello world are same
d after strcmp is 0
string hello world and india are not same
d after strcmp is -1
string hello world and india are not same
d after strcmp is 1
o found in string hello world and return is o world
w found in string hello world and return is world
c after strcat is india says,hello world

All the usage should be self evident except why the strcmp return 0, -1 and then 1. strcmp actually return the difference in ASCII value of the first charecter in difference.

  1. strcmp of "hello world" and "hello world" return 0 as no difference.
  2. strcmp of "hello world" and "india" return -1 as ASCII value of h - ASCII value of i is is -1
  3. strcmp of "india" and "hello world" return 1 as ASCII value of i - ASCII value of h is 1.

Hope usage of other example are self evident.

Links

No comments :

Post a Comment