a.Name your program hw13b.c b.The function proto-type will look like: char *lcsu
ID: 3625551 • Letter: A
Question
a.Name your program hw13b.c
b.The function proto-type will look like:
char *lcsuffix(char str1[], char str2[]);
where str1[] and str2[] are the two input strings, and the returned value is the address of the first element of the longest common suffix.
c. Write and call the function above repeatedly using a do-while loop. Below is a
sample run:
Input two words separated by a space: globally internally
The longest common suffix is "ally".
Again (Y/N)? Y
Input two words separated by a space: gloves dove
The longest common suffix is "".
Again (Y/N)? N
Note that to print " in a printf() statement, you need to put " in the format string.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<string.h>
char *lcsuffix(char str1[], char str2[])
{
int len,i;
char *p = str1;
for(i=0; i<strlen(str1); i++)
{
if(strstr(str2,p))
{
len = strlen(p);
break;
}
p++;
}
return p;
}
int main()
{
char str1[20];
char str2[20];
char op;
do
{
printf("Input two words separated by a space: ");
scanf("%s %s",str1,str2);
printf("The longest common suffix is "%s"", lcsuffix(str1,str2));
printf(" Again (Y/N)? ");
fflush(stdin);
scanf("%c",&op);
}while(op=='Y');
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.