× Home
Next Lec → ← Previous Lec

String functions and <string.h> library

C Library: <string.h>

Till now we have only used <stdio.h> library but there are more.
There are many functions in a library and we can't remember all of them so just practise the basic one and take help from google.

We can use the funtions from string.h to manipulate strings.

strcat()

                                

                                        #include <stdio.h>
                                        #include <string.h>
                                        int main()
                                        {
                                            char string1[]="harry";
                                            char string2[]="ravi";
                                            puts(strcat(string1,string2));
                                            return 0;
                                        } 
                                
                            

harryravi

strlength()

                                

                                        #include <stdio.h>
                                        #include <string.h>
                                        int main()
                                        {
                                            char string1[] = "harry";
                                            printf("The length of string1 is : %d", strlength(string1));
                                            // this print 5 which means null character is not included
                                            return 0;
                                        }
                                
                            

The length of string1 is : 5

strrev()

                            

                            #include <stdio.h>
                            #include <string.h>
                            int main()
                            {
                                char string1[]="harry";
                                printf("The reversed string string1 is: %s", strrev(string1));
                                return 0;
                            }
                        
                        

The reversed string string1 is: yrrah

strcpy()

                            
                                
                            #include <stdio.h>
                            #include <string.h>
                            int main()
                            {
                                char string1[]="harry";
                                char string2[]="ravi";
                                char string3[10];
                                puts(strcpy(string3,strcat(string1,string2)));
                                return 0;
                            }
                        
                        

harryravi

strcmp()

                            

                            #include <stdio.h>
                            #include <string.h>
                            int main()
                            {
                                char string1[]="harry";
                                char string2[]="ravi";
                                printf("The strcmp for string1, string2 returned : %d", strcmp(string1,string2));
                                //We can't expect ASCII value from this. It returns 0 if both string are same.
                                return 0;
                            }
                        
                        

The strcmp for string1, string2 returned : -1

Homework

Allow user to enter two strings and then concatenate them by saying that
str1 is a friend of str2

                        
                        
                        #include <stdio.h>
                        #include <string.h>
                        int main()
                        {
                            char str1[20];
                            char str2[20];
                            printf("Enter person one name : ");
                            gets(str1);
                            printf("\nEnter person two name : ");
                            gets(str2);
                            printf("\n%s is a friend of %s",str1,str2);
                            return 0;
                        }