Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program to compare strings, concatenate strings, copy string and display part of a string

Posted By: Owen Evans     Category: C Programming     Views: 21502

Write a menu driven c program which performs the following string operations
- Find the Length of the string
- Concatenate two strings
- Compare two string and display if they are equal
- Copy one string from the other
- Extract a part of the string (starting point and no of characters to be extracted as input)

Code for Program to compare strings, concatenate strings, copy string and display part of a string in C Programming

       # include<stdio.h>
       void main()
       {
           int c;
           void func1();
           void func2();
           void func3();
           void func4();
           void func5();
           clrscr();
           printf("\n- : User Defined String Functions : -");
           printf("\n-------------------------------------");
           printf("\n Find String Length      : 1");
           printf("\n Concatenate two Strings : 2");
           printf("\n String Comparison       : 3");
           printf("\n String Copy             : 4");
           printf("\n Extract SubString       : 5");
           printf("\n Enter Your Choice       : ");
           scanf("%d",&c);
           switch(c)
           {
               case 1:
                   func1();
                   break;
               case 2:
                   func2();
                   break;
               case 3:
                   func3();
                   break;
               case 4:
                   func4();
                   break;
               case 5:
                   func5();
                   break;
               default:
                   printf("\nInvalid Choice");
           }
           getch();
       }
       void func1()
       {
           char str[50];
           int slength(char []);
           int l;
           clrscr();
           fflush(stdin);
           printf("\nEnter String : ");
           scanf("%[^\n]",str);
           l=slength(str);
           printf("\nLength = %d",l);
       }
       int slength(char x[])
       {
           int i=0;
           while(x[i]!='\0')
               i++;
           return i;
       }
       void func2()
       {
           char str1[50],str2[50];
           void sconcate(char [],char []);
           clrscr();
           fflush(stdin);
           fflush(stdin);
           printf("\nEnter String 1 : ");
           scanf("%[^\n]",str1);
           fflush(stdin);
           printf("\nEnter String 2 : ");
           scanf("%[^\n]",str2);
           sconcate(str1,str2);
           printf("%s",str1);
       }
       void sconcate(char s1[],char s2[])
       {
           int i=0,j;
           j=slength(s1);
           while(s2[i]!='\0')
           {
               s1[j]=s2[i];
               j++;
               i++;
           }
           s1[j]='\0';
       }
       void func3()
       {
           char str1[50],str2[50];
           int l;
           int scomp(char [],char []);
           clrscr();
           fflush(stdin);
           printf("\nEnter String 1 : ");
           scanf("%[^\n]",str1);
           fflush(stdin);
           printf("\nEnter String 2 : ");
           scanf("%[^\n]",str2);
           l=scomp(str1,str2);
           if(l==0)
               printf("\nBoth Strings are Equal");
           elseif(l>0)
               printf("\nString 1 is greater than String 2");
           else
               printf("\nString 2 is greater than String 1");
       }
       int scomp(char s1[],char s2[])
       {
           int i,j,z;
           int l;
           for(i=0,j=0;(s1[i]!='\0' || s2[i]!='\0');i++,j++)
           {
               if(s1[i]!=s2[i])
               {
                   if(s1[i]>s2[i])
                       l=1;
                   else
                       l=-1;
                   break;
               }
           }
           if(s1[i]=='\0' && s2[i]=='\0')
               l=0;
           return l;
       }
       void func4()
       {
           char str1[50],str2[50];
           void scopy(char [],char []);
           clrscr();
           fflush(stdin);
           printf("\nEnter String : ");
           scanf("%[^\n]",str1);
           scopy(str2,str1);
           printf("\nCopied String : %s",str2);
       }
       void scopy(char s2[],char s1[])
       {
           int i;
           i=0;
           while(s1[i]!='\0')
           {
               s2[i]=s1[i];
               i++;
           }
           s2[i]='\0';
       }
       void func5()
       {
           char str[50],sub[50];
           int s,n;
           void substr(char [],int,int,char []);
           clrscr();
           fflush(stdin);
           printf("\nEnter string : ");
           scanf("%[^\n]",str);
           fflush(stdin);
           printf("\nEnter substring starting character index : ");
           scanf("%d",&s);
           fflush(stdin);
           printf("\nEnter total no. of characters : ");
           scanf("%d",&n);
           substr(str,s,n,sub);
           printf("\nSubstring = %s",sub);
       }
       void substr(char str[],int s,int n,char ds[])
       {
           int i,j;
           j=0;
           i=s;
           while( (i<(s+n)) && (str[i]!='\0'))
           {
           ds[j]=str[i];
           i++;
           j++;
           }
           ds[j]='\0';
       }
  
Share: 



Owen Evans
Owen Evans author of Program to compare strings, concatenate strings, copy string and display part of a string is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!