A famous editor of English dictionaries are in the process of digitalizing their
ID: 3725522 • Letter: A
Question
A famous editor of English dictionaries are in the process of digitalizing their dictionary collection. Their goal is run some language processing algorithms in order to help academics researching the English language.
One particular algorithm they would like to run is finding the number of words that share the same suffix. As a start, they would like to determine the longest suffix two words share.
Write program suffix.c that displays the longest suffix two words share, as illustrated in the following example:
Here are a list of requirements, assumptions and hints:
This program shall contain no global variables.
We assume that the maximum number of characters a word can contain is 30.
While the two strings that are being filled out by the user are local arrays in the main function, any additional strings created during the execution should be dynamically allocated in the heap.
Do not forget that dynamically allocated memory should be freed appropriately.
You must implement function char *find_suffix(char *s1, char *s2) that returns a dynamically allocated string containing the longest suffix shared by strings s1 and s2.
Hint: you should start iterating from the end of the words.
Hint: for loops allows multiple initializations and updates within their construct, if they are separated with commas.
For example, you can iterate on both some i and j variables until one of them (or both) fails a condition:
You should probably a similar construct for iterating through the two strings at the same time.
The main function should get the user input, then run the suffix function, and finally display the suffix.
List of some important libc functions that are used in the reference program: strlen(), malloc(), free(), scanf().
follow the written guidelines if possible.
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// function to compute the total length of the suffix sring
int find_suffix(char * s1, char * s2)
{
int len1= strlen(s1), len2 = strlen(s2), temp, value=0;
// stroing length of shortest of both strings
if(len1 > len2)
temp = len2;
else
temp = len1;
//run from length of string (excluding the /0) to shortest string
for(int i=(len1-1), j=(len2-1); temp >0; i--,j--,temp--)
{
if (s1[i] == s2[j])
value++; // incrementing the value if char match
}
return value;
}
int main(int argc, char **argv) {
// to check if user has passed 2 arguements or not
if (argc != 3)
{
printf("You have not passed enough areguemnts to program for compare, Pass 2 strings. ");
return 1;
}
char *first = (char *) malloc(strlen(argv[1]) + 1); //strlen returns the size of the string not including the null character at the end.
strcpy(first,argv[1]);
char *second = (char *) malloc(strlen(argv[2]) + 1); //strlen returns the size of the string not including the null character at the end.
strcpy(second,argv[2]);
// getting value from function of longest suffix and stroing it in size
int size= find_suffix(first, second);
// printf("The value of size is %d ", size);
// if length is 0
if(size == 0)
{
printf("No suffix matched between strings !!! ");
return 1;
}
// in case there is suffix matched between 2 strings
else
{
printf("Common suffix between '%s' and '%s' is ", first,second );
// printing the suffix
for (int i = strlen(first)-size; i < strlen(first); ++i)
printf("%c", first[i]);
}
printf(" ");
// free the dynamically space allocated to the variables
free(first);
free(second);
return 0;
}
Output of the code:
dps@machine:~/Documents/Chegg$ gcc longest_suffix.c -o longest_suffix
dps@machine:~/Documents/Chegg$ ./longest_suffix hello pello
Common suffix between 'hello' and 'pello' is ello
dps@machine:~/Documents/Chegg$ ./longest_suffix instagram datagram
Common suffix between 'instagram' and 'datagram' is tagram
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.