C Program to implement the Basic String Copy And String Compare Functions..

#include<stdio.h>

int strcmps(char *s,char *t)
{
 int ans=0;
 int i;
 for(i=0;s[i]!='\0'&&t[i]!='\0';i++)
 {
  if(s[i]!=t[i])
  {
   ans=(s[i]-t[i]);
  }
 }
 return ans;
}

void scopy(char *t,char *s)
{
 int i;
 for(i=0;s[i]!='\0';i++)
  t[i]=s[i];
 t[i]='\0';
}
void main()
{
 char a[50],b[50];
 int ch;
 printf("\nChoice : 1) String copy \n  2) String compare ");
 printf("\nEnter your Choice..");
 scanf("%d",&ch);
 if(ch==1)
   {
    printf("\nEnter the String..");
    scanf("%s",a);
    scopy(b,a);
    printf("\nThe copied String..");
    printf("%s",b);
   }
 else if(ch==2)
   {
   printf("\nEnter the first String..");
   scanf("%s",a);
   printf("\nEnter the second String..");
   scanf("%s",b);
   if(strcmps(a,b)>0)
     printf("The first String is greater than the second String..");
   else if(strcmp(a,b)<0)
     printf("The first String is lesser than the second String..");
   else
     printf("The first String is equal to  the second String..");
  }
 else
   printf("\nPlease Enter a valid choice..");;
}

No comments:

Post a comment