String Handling Programs
| Program to find length of the string. | ||||||
| #include <stdio.h> #include <conio.h> #include <string.h> void main () { char s[20]; int l; clrscr (); puts(“Enter String”); gets(s); l = strlen (s); printf (“Length=%d”, l); getch (); } Output Enter String WELCOME Length=7 |
||||||
| Program to copy one string into another string. | ||||||
| #include <stdio.h> #include <conio.h> #include <string.h> void main () { char s1[20],s2[20]; clrscr (); puts(“Enter String”); gets(s1); strcpy(s2, s1); printf (“S1=%s\n”, s1); printf (“S2=%s\n”, s2); getch (); } Output Enter String Hello S1=Hello S2=Hello |
||||||
| Program to concate two strings. | ||||||
| #include <stdio.h> #include <conio.h> #include <string.h> void main () { char s1[40],s2[20]; clrscr (); puts(“Enter String1”); gets(s1); puts(“Enter String2”); gets(s2); strcat(s1, s2); printf (“S1=%s\n”, s1); getch (); } Output Enter String1 Hello Enter String2 Welcome S1=HelloWelcome |
||||||
| Program to compare two strings. | ||||||
| #include <stdio.h> #include <conio.h> #include <string.h> void main () { char s1[20],s2[20]; int ans; clrscr (); puts(“Enter String1”); gets(s1); puts(“Enter String2”); gets(s2); ans=strcmp(s1, s2); if (ans == 0) printf (“String are equal); else printf("String are Not equal"); getch (); } Output Enter String1 MAX Enter String2 MIN String are not equal |
||||||
| Program to reverse given string. | ||||||
| #include <stdio.h> #include <conio.h> #include <string.h> void main () { char s1[20]; clrscr (); puts(“Enter String”); gets(s1); printf (“Original String is %s\n”, s1); strrev(s1); printf (“Reverse is %s\n”, s1); getch (); } Output Enter String NODE Original String Is NODE Reverse is EDON |
||||||
| Program to find one string into another string. | ||||||
| #include <stdio.h> #include <conio.h> #include <string.h> void main () { char s1[20],s2[20]; clrscr (); puts(“Enter Original String”); gets(s1); puts(“Enter Search String”); gets(s2); if ( strstr(s1, s2)==NULL) printf (“String Not Found"); else printf (“String Found”); getch (); } Output Enter Original String Hello How Are U Enter Search String How String Found |