C Program For Operations on Strings (User defined functions)

#include<stdio.h>
#include<string.h>
#include<conio.h>2
void concat(char[], char[]);
void compare(char[], char[]);
void reverse(char[]);
void length(char[]);
void main()
{
char s1[10],s2[10];
int i,opt,j;
clrscr();
l: printf("\n\nSelect any one of the given Options:\n");
printf("\n        Menu\n\n0.Length of the String\n1.String Reverse\n2.String Concatenation\n3.String Comparison\n4.Exit:\n");
scanf("%d",&opt);
switch(opt)
{
case 0:printf("Enter the string:");
       scanf("%s",s1);
       for(i=0;s1[i]!='\0';i++);
       printf("Length of the string is: %d characters",i);
break;
case 1:printf("Enter the string:");
       scanf("%s",s1);
       reverse(s1);
break;
case 2: printf("Enter 1st string:");
 scanf("%s",s1);
 printf("Enter 2nd string:");
 scanf("%s",s2);
 concat(s1,s2);
break;
case 3: printf("Enter 1st string:");
       scanf("%s",s1);
 printf("Enter 2nd string:");
       scanf("%s",s2);
 compare(s1,s2);
break;
case 4: exit(0);
default:printf("Choose the correct option\n");
}
goto l;
}
 void concat(char s1[], char s2[])
 {
 int i,j;
 i=strlen(s1);
 for(j=0;s2[j]!='\0';i++,j++)
 s1[i]=s2[j];

 s1[i]='\0';
 printf("Resultant String is: %s",s1);
 }
  void compare(char s1[], char s2[])
  {
   int i=0,flag;
   while(s1[i]!='\0'||s2[i]!='\0')
   {
   if(s1[i]==s2[i])
   {
   flag=0;
   }
   else
   {
   flag=1;
   break;
   }
   i++;
   }
   if(flag==0)
   printf("Both the Strings are Equal\n");
   else
   {
   printf("Given Strings are not Equal\n");
   }
   }
         void reverse(char s1[])
         {
         int i=0,j,temp;
         j=strlen(s1)-1;
    while(i<j)
    {
    temp=s1[i];
    s1[i]=s1[j];
    s1[j]=temp;
    i++;
    j--;
    }
    printf("Reverse of given string is: %s ",s1);
    }







Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors and Social Networking. Feel Free to let me know any of your concerns about hacking or let me know if you need any more methods on hacking anything. Enjoy Learning

No comments:

Post a Comment