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
- Find the length of string -
strlen
- Copy a string -
strcpy
- Compare two string -
strcmp
- Find a character in string -
strchr
- Find sub-string in string -
strstr
- 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
size_t
- It is new type which can store length. (Mostly typedef of unsigned int)- All string that a function modifies are
char*
- 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.
strcmp
of "hello world" and "hello world" return 0 as no difference.strcmp
of "hello world" and "india" return -1 as ASCII value of h - ASCII value of i is is -1strcmp
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
- Next Article - C Programming #74: strlen implementation
- Previous Article - C Programming #72: Strings
- All Article - C Programming
No comments :
Post a Comment