String Operations in C Program
Problem Statement
Source Code to Implement String Operations in C Program or String Operation in C and C++ Language . Operations on String are as Follows :
- Input Operations – This operation will ask for input from user.
- Display Operations – This will display user the output.
- Palindrome Operations – this function will check the entered string for palindrome.
- Copy String – This option will make a copy of string.
- Compare Operations on String – This option will compare the two entered strings according to requirement.
- Reverse Strings – This function will reverse the string and then print to the screen.
- Concatenate Operations on String – This function will concatenate two strings and then print the final string.
- Finding Sub string Operations on String
Operations implementation by two methods
- Using Pointers
- Without Pointers
String Operations in C Program Code
#include #include void withp(char []); void withoutp(char []); void in (char []); void out(char []); void palindrome(char []); void reverse(char []); void substring(char []); void compare(char []); void concat (char []); int length (char []); void copy(char []); void input(char *); void display(char *); void palindrome1(char *); void reverse1(char *); void compare1(char *); void concat1 (char *); void substring1 (char *); int length1 (char *); void copy1(char *); void main() { int ch; char str[50]; str[0]=NULL; clrscr(); do { clrscr(); printf("\n\n\n\t\t-------: MAIN MENU :--------"); printf("\n\n\n\t1\tUSING POINTERS \n "); printf("\n\t2\tWITHOUT USING POINTERS\n"); printf("\n\t3\tEXIT\n\n"); printf("\n\tENTER UR CHOICE\t: "); scanf("%d",&ch); switch(ch) { case 1: withp(str); break; case 2:withoutp(str); break; case 3: break; default: printf("\n\n\t!!!!!! INVALID CHOICE !!!!!!!"); getch(); } }while(ch!=3); getch(); } //END OF MAIN /********************************************************************* WITH POINTERS FUNCTOIN ************************************************************************/ void withp(char str[]) { int ch,s1=0; do { clrscr(); printf("\n\n\t\t------: USING POINTER MENU :------\n\n"); printf("\n\n\t1\tINPUT"); printf("\n\n\t2\tDISPLAY"); printf("\n\n\t3\tLENGHT OF THE STRING"); printf("\n\n\t4\tPALINDROME"); printf("\n\n\t5\tCOPY THE STRING"); printf("\n\n\t6\tCOMPARE THE STRING "); printf("\n\n\t7\tREVERSE"); printf("\n\n\t8\tCONCATINATE THE STRING"); printf("\n\n\t9\tFIND THE SUBSTRING"); printf("\n\n\t10\tRETURN TO MAIN MENU"); printf("\n\n\n\tENTER UR CHOICE \t: "); scanf("%d",&ch); switch(ch) { case 1: input(str); break; case 2: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } display(str); getch(); break; case 3: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } s1=length1(str); printf("\n\n\tLENGTH OF THE STRING IS %d",s1); getch(); break; case 4: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } palindrome1(str); break; case 5: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } copy(str); break; case 6: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } compare1(str); break; case 7: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } reverse1(str); break; case 8: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } concat1(str); break; case 9: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } substring1(str); break; case 10: break; default: printf("\n\n\t!!!! INVALID CHOICE !!!!!"); getch(); } }while(ch!=10); } /***************************************************************** INPUT FUNCTION *******************************************************************/ void input(char *str) { int i=0; printf("\n\n\tENTER THE STRING\t"); do { flushall(); *(str+i++)=getche(); } while(*(str+i-1)!='\r'); *(str+i)=NULL; } /***************************************************************** DISPLAY FUNCTION *******************************************************************/ void display(char *str) { int i=0; printf("\n\n\t\t"); while(*(str+i)!='\r') { printf("%c",*(str+i++)); } getch(); } /***************************************************************** PALINDROME FUNCTION *******************************************************************/ void palindrome1(char *str) { int s,e,l=0; l=length1(str); for(s=0,e=l-1;s<e;s++,e--) <br=""> if(*(str+s)!=*(str+e)) break; if(s==e) printf("\n\n\t!!!!!!!! PALINDROME !!!!!!!!"); else printf("\n\n\t!!!!!!!! NOT PALINDROME !!!!!!!!"); getch(); } /***************************************************************** REVERSE FUNCTION *******************************************************************/ void reverse1(char *str) { int a,s,e,l=0; l=length1(str); for(s=0,e=l-1;s<e;s++,e--) <br=""> { a=*(str+s); *(str+s)=*(str+e); *(str+e)=a; } printf("\n\n\t!!!!!!!!!! STRING HAS BEEN REVERSED !!!!!!!!!"); printf("\n\n\tREVERSED STRING :-->> "); display(str); } /***************************************************************** CONCATINATION FUNCTION *******************************************************************/ void concat1 (char *str) { int i=0,l1; char *s1; input(s1); l1=length1(str); while(*(s1+i)!='\r') *(str+i+l1)=*(s1+i++); *(str+i+l1)='\r'; *(str+i+l1+1)=NULL; printf("\n\n\tCONCATINATED STRING :-->> "); display(str); } /***************************************************************** LENGTH FUNCTION *******************************************************************/ int length1(char *str) { int i; for(i=0;*(str+i)!=NULL;i++); return --i; } /***************************************************************** COMPARE FUNCTION *******************************************************************/ void compare1(char *st) { int i,j,l1=0,l2=0; char *s1; input(s1); l1=length1(st); l2=length1(s1); if(l1==l2) { for(i=0;i<l1;i++) <br=""> if(*(st+i)!=*(s1+i)) { if(*(st+i) > *(s1+i)) printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND "); else printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST "); break; } if(i==l2) printf("\n\n\tBOTH THE STRINGS R EQUAL"); } else { if(l1>l2) printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND "); else printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST "); } getch(); } /***************************************************************** COPY FUNCTION *******************************************************************/ void copy1(char *st) { int i,j; char *s1; input(s1); for(i=0;*(st+i)!=NULL;i++) *(s1+i) = *(st+i); *(s1+i)=NULL; printf("\tTHE STRING IS COPIED"); display(s1); getch(); } /***************************************************************** SUBSTRING FUNCTION *******************************************************************/ void substring1(char *st) { int j,i,m=0,l2=0; char *s1; input(s1); l2=length1(s1); for(i=0;*(st+i)!=NULL;i++) if(*(st+i)==*(s1)) { for(j=1;j<l2;j++) <br=""> if(*(st+i+j)!=*(s1+j)) break; if(j==l2) m++; } if(m>0) printf("\n\n\t\tTHE STRING YOU HAVE ENTERED IS PRESENT %d TIMES",m); else printf("\nTHE STRING YOU HAVE ENTERED IS NOT A SUBSTRING OF THE STRING\n"); getch(); } /******************************************************* WITHOUT POINTERS ********************************************************/ void withoutp(char str[]) { int ch,s1=0; clrscr(); do { clrscr(); printf("\n\n\t\t------: WITHOUT USING POINTER MENU :------\n\n"); printf("\n\n\t1\tINPUT"); printf("\n\n\t2\tDISPLAY"); printf("\n\n\t3\tLENGHT OF THE STRING"); printf("\n\n\t4\tPALINDROME"); printf("\n\n\t5\tCOPY THE STRING"); printf("\n\n\t6\tCOMPARE THE STRING "); printf("\n\n\t7\tREVERSE"); printf("\n\n\t8\tCONCATINATE THE STRING"); printf("\n\n\t9\tFIND THE SUBSTRING"); printf("\n\n\t10\tRETURN TO MAIN MENU"); printf("\n\n\n\tENTER UR CHOICE \t: "); scanf("%d",&ch); switch(ch) { case 1: in(str); break; case 2: if(str[0]==NULL) { printf("\n\n\t!!!!! ENTER THE STRING "); getch(); break; } printf("\n\n\tTHE STRING IS \n\n\t\t "); out(str); break; case 3: if(str[0]==NULL) { printf("\n\n\t!!!!!!! ENTER THE STRING "); getch(); break; } s1=length(str); printf("\n\n\tLENGTH OF THE STRING IS %d",s1); getch(); break; case 4: if(str[0]==NULL) { printf("\n\n\t!!!!!!!!! ENTER THE STRING "); getch(); break; } palindrome(str); break; case 5: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } copy(str); break; case 6: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } compare(str); break; case 7: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } reverse(str); break; case 8: if(s1==0) { printf("\n\n\tENTER THE STRING "); getch(); break; } concat(str); break; case 9: if(str[0]==NULL) { printf("\n\n\tENTER THE STRING "); getch(); break; } substring(str); break; case 10: break; default: printf("\n\n\t!!!! INVALID CHOICE !!!!!"); getch(); } }while(ch!=10); } /***************************************************************** INPUT FUNCTION *******************************************************************/ void in(char st[]) { int i=0; printf("\n\n\tENTER THE STRING\t"); do { flushall(); st[i++]=getche(); } while(st[i-1]!='\r'); st[i]=NULL; } /***************************************************************** OUTPUT FUNCTION *******************************************************************/ void out(char st[]) { int i=0; printf("\n\n\t\t"); while(st[i]!=NULL) { printf("%c",st[i++]); } getch(); } /***************************************************************** PALINDROME FUNCTION *******************************************************************/ void palindrome (char st[]) { int s,e,l=0; l=length(st); for(s=0,e=l-1;s<e;s++,e--) <br=""> if(st[s]!=st[e]) break; if(s==e) printf("\n\n\t!!!!!!!! PALINDROME !!!!!!!!"); else printf("\n\n\t!!!!!!!! NOT PALINDROME !!!!!!!!"); getch(); } /***************************************************************** REVERSE FUNCTION *******************************************************************/ void reverse (char st[]) { int a,s,e,l=0; l=length(st); for(s=0,e=l-1;s<e;s++,e--) <br=""> { a=st[s]; st[s]=st[e]; st[e]=a; } printf("\n\n\t!!!!!!!!!! STRING HAS BEEN REVERSED !!!!!!!!!"); printf("\n\n\tREVERSED STRING :-->> "); out(st); } /***************************************************************** COPY FUNCTION *******************************************************************/ void copy(char st[]) { int i,j,l=0; char s1[50]; l=length(st); for(i=0;i<l;i++) <br=""> s1[i]=st[i]; s1[i]=NULL; printf("\tTHE STRING IS COPIED"); out(s1); getch(); } /***************************************************************** COMPARE FUNCTION *******************************************************************/ void compare(char st[]) { int i,j,l1=0,l2=0; char s1[50]; in(s1); l1=length(st); l2=length(s1); if(l1==l2) { for(i=0;i<l2;i++) <br=""> if(st[i]!=s1[i]) { if(st[i]>s1[i]) printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND "); else printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST "); break; } if(i==l2) printf("\n\n\tBOTH THE STRINGS R EQUAL"); } else { if(l1>l2) printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND "); else printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST "); } getch(); } /***************************************************************** SUBSTRING FUNCTION *******************************************************************/ void substring (char st[]) { int i,j,m=0,l1=0,l2=0; char s1[50]; in(s1); l1=length(st); l2=length(s1); for(i=0;i<l1;i++) <br=""> { if(st[i]==s1[0]) { for(j=0;j<l2;j++) <br=""> if(st[i+j]!=s1[j]) break; if(j==l2) m++; } } if(m!=0) printf("\n\n\t SUBSTRING IS PRESENT %d TIMES IN THE STRING",m); else printf("\n\n\tTHE SUBSTRING IS NOT PRESENT"); getch(); } /***************************************************************** CONCATINATION FUNCTION *******************************************************************/ void concat (char st[]) { int i=0,l1=0; char s1[50]; l1=length(st); while(s1[i]!='\r') st[i+l1]=s1[i++]; st[i+l1]=NULL; printf("\n\n\tCONCATINATED STRING :-->> "); out(st); } /***************************************************************** LENGTH FUNCTION *******************************************************************/ int length (char st[]) { int i; for(i=0;st[i]!=NULL;i++); return --i; } /***************************************************************** THANKS *******************************************************************/
Leave a Reply