Wednesday, March 3, 2021

Menu-driven program to compare, concatenate, palindrome checking, length calculation, copy strings using structure

 
 #include<stdio.h>
 #include<string.h>
struct string1

 char str1 [40], str2[20];
 };
 
 void intake(struct string1);
  void compare(struct string1);
  void copy(struct string1);
  void concatenate(struct string1);
  void palindrome(struct string1);
  void len(struct string1);

 void main ()
 { 
  struct string1 obj;
 intake(obj);  
  getch ();
  }

  void palindrome(struct string1 obj)
  {
   int i=0,len;
   while(obj.str1[i]!='\0')
   i++;
   len=i-1;
   i=0;
   while(obj.str1[i]!='\0')
   {
   if(obj.str1[i]!=obj.str1[len])  
    break;
    i++;
    len--;  
   }
  
   if(len==-1)
  printf("\n1st string is palindrome");
  else
  printf("\n1st string is not palindrome");  
 i=0;
   while(obj.str2[i]!='\0')
   i++;
   len=i-1;
   i=0;
   while(obj.str2[i]!='\0')
   {
   if(obj.str2[i]!=obj.str2[len])
  
    break;
    i++;
    len--;  
   }
   if(len==-1)
  printf("\n2nd string is palindrome");
  else
  printf("\n2nd string is not palindrome"); 
 
 } 
  
   void intake(struct string1 obj)
  {  
   printf("\nstring1 No 1: ");
   gets(obj.str1);
   fflush(stdin);
   printf("\nstring1 No 2: ");
   gets(obj.str2);   
   printf("\nStrings as entered\n\n");
 printf("\n\n\n");
  printf("\n   %s   %s", obj.str1, obj.str2);
    palindrome(obj);
    printf("\n\n\n");  
  printf("\nLength of strings\n\n");
    len(obj);    
   printf("\n\n\n");  
  printf("\nCalling compare function\n\n");
    compare(obj);
   printf("\n\n\n");  
  printf("\nCalling concatenate function\n\n");
    concatenate(obj);
     printf("\n\n\n");  
  printf("\nCalling copy function\n\n");
    copy(obj);   
}

void len(struct string1 obj)
{
int i=0,j=0;
while(obj.str1[i]!='\0')
i++;
printf("\nLength of %s is %d",obj.str1,i);
while(obj.str2[j]!='\0')
j++;
printf("\nLength of %s is %d",obj.str2,j);
if(i>j)
printf("\nLength wise %s is greater",obj.str1);
else if(j>i)
printf("\nLength wise %s is greater",obj.str2);
else
printf("\nLength wise both are same");
}

void compare(struct string1 obj)
{
int i=0;
while(obj.str1[i]!='\0')

   {
if(obj.str1[i]!=obj.str2[i])
   break;
  i++;
}
  if(obj.str1[i]>obj.str2[i])
 printf("\n Greater string is %s", obj.str1);
 else if(obj.str1[i]<obj.str2[i])
 printf("\n Greater string is %s", obj.str2);
 else
 printf("\nBoth are same");
}

void copy(struct string1 obj)
{
int i=0;
while(obj.str2[i]!='\0')
   {
   obj.str1[i]=obj.str2[i];  
  i++;
}
  obj.str1[i]='\0';
 printf("\n 2nd string is copied on 1st string");
 printf("\n   %s   %s", obj.str1, obj.str2);  
}

void concatenate(struct string1 obj)
{
int i=0,x=0;
while(obj.str1[i]!='\0')
   {  
  i++;
}   
while(obj.str2[x]!='\0')
  {  
   obj.str1[i]=obj.str2[x];
   i++;
   x++;
  }
  obj.str1[i]='\0';
 printf("\n After concatenation");
 printf("\n   %s  ", obj.str1);
}


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner