Question
In C programming Language

8. Write and test a function that finds and returns through an output parameter the longest common suffix of two words (e.g.,
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   //Variable declaration
   char s1[20],s2[20],i,len,lenl,s[20],j=0;
   //Reading string from user
   printf("Enter the First string\n");
   gets(s1);
   printf("Enter the Second string\n");
   gets(s2);
   //Comparing the string for equality
   //IF both the string are equal the whole string will consider as suffix
   if(strcmp(s1,s2)==0){
       printf("Suffix is %s \n",s1);
   }
   //To minimise the iteration finding the smaller string s1 is smaller then below code
   else if(strcmp(s1,s2)<0){
       len=strlen(s1);
       lenl=strlen(s2);
       //loop will executed based on length of s1
       for(i=len-1;i>=0;i--){
           //Comparing each character and it it is same storing into another array
           if(s1[i]==s2[lenl-1]){
               s[j]=s1[i];
               j++;//index of new array
           }
           else{
               break;
           }
           lenl--;
       }
   }
   else{
       //s2 is the smallest string
       len=strlen(s2);
       lenl=strlen(s1);
       //loop will executed based on length of s1
       for(i=len-1;i>=0;i--){
           if(s2[i]==s1[lenl-1]){   //Comparing each character and it it is same storing into another array
               s[j]=s2[i];
               j++;
           }
           else{
               break;
           }
           lenl--;
       }
   }
   //Checking whether any match found
   if(strlen(s)<0){
       printf("No commmon suffix\n");
   }
   else{//printing the suffix
       for(j=j-1;j>=0;j--){
           printf("%c",s[j]);
       }
   }
   getch();
  
}D:\c\LArgestsuffix.exe Enter the First string procrastination Enter the Second string destination stinationD:\c\LArgestsuffix.exe Enter the First string globally Enter the Second string internally ally

Add a comment
Know the answer?
Add Answer to:
In C programming Language 8. Write and test a function that finds and returns through an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT