Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\"my C programming lab\". I had been worked on this lab over the break, but stil

ID: 3684521 • Letter: #

Question

"my C programming lab". I had been worked on this lab over the break, but still confused and driving me nuts. I'll be grateful if someone could help me.

Suffix Examples:

str1 is “happy”         str2 is “ppy”                          SUFFIX

str1 is “university” str2 is “sity”                             SUFFIX

str1 is “happy” str2 is “apy”                                NOT SUFFIX

str1 is “university” str2 is “siTy”                         NOT SUFFIX

Reverse Substring Examples:

str1 is “happy”          str2 is “ppa”                          REVERSE SUBSTRING

str1 is “university” str2 is “rev”                             REVERSE SUBSTRING

str1 is “happy” str2 is “bap”                                NOT REVERSE SUBSTRING

str1 is “university” str2 is “nIve”                         NOT REVERSE SUBSTRING

(#)In this lab we will work with strings (so we will be using character arrays).

Write a program that reads two strings str1 and str2 from the keyboard

Print both strings.

Write a function computeLength (str) and call this function in main to compute the length of the string str1 and str2.

If the string str1 is of length 1 the program will terminate

If the string str2 is of length 0 the program will terminate

If length of str2 is greater than the length of str1 the program will terminate

Now write a function isSuffix (str1, str2). If the string str2 is a suffix of str1 thenisSuffix should return a 1, if not return a zero. Output the correct conclusion either SUFFIX or NOT SUFFIX.

Now write a function isReverseSubstring (str1, str2) that returns a 1 if str2 is a reverse substring of str1 and 0 otherwise. Output the correct conclusion either REVERSE SUBSTRING or NOT REVERSE SUBSTRING

You are NOT to use any standard library functions except for I/O functions.

Here is a potential list of functions you may want to declare

int   computeLength (char *str);

int   isSuffix (char *str1,   char *str2);

int   isReverseSubstring (char *str1, char *str2);

Explanation / Answer

Here is the code for you:

#include <stdio.h>
#include <string.h>
int computeLength (char *str)
{
int count = 0;
while(*(str+count) != '')
count++;
return count;
}
int isSuffix (char *str1, char *str2)
{
int len1 = computeLength(str1);
int len2 = computeLength(str2);
int j;
for(int i = len2-1; i >= 0; i--)
{
j = len2-i-1;
if(*(str1+len1-j-1) != *(str2+len2-j-1))
return 0;
}
return 1;
}
int isReverseSubstring (char *str1, char *str2)
{
int len1 = computeLength(str1);
int len2 = computeLength(str2);
int j;
for(int i = 0; i <= len1-len2; i++)
{
for(j = len2-1; j >= 0; j--)
{
if(*(str1+len2-1-j) != *(str2+j))
break;
}
if(j == -1)
return 1;
}
return 0;
}
int main()
{
char str1[50], str2[50];
//Write a program that reads two strings str1 and str2 from the keyboard
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
//Print both strings.
printf("String 1: %s ", str1);
printf("String 2: %s ", str2);
int str1Length = computeLength(str1);
int str2Length = computeLength(str2);
//If the string str1 is of length 1 the program will terminate
if(str1Length == 1)
return 0;
//If the string str2 is of length 0 the program will terminate
if(str2Length == 0)
return 0;
//If length of str2 is greater than the length of str1 the program will terminate
if(str2Length > str1Length)
return 0;
printf("String1 length: %i, String2 length: %i ", str1Length, str2Length);
if(isSuffix(str1, str2))
printf("SUFFIX ");
else
printf("NOT SUFFIX ");
if(isReverseSubstring(str1, str2))
printf("REVERSE SUBSTRING ");
else
printf("NOT REVERSE SUBSTRING ");   
}

If you need any refinements, just get back to me.