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

Lab 8 (this is to be turned in) Palindrome is a string that is the same as backw

ID: 3727435 • Letter: L

Question

Lab 8 (this is to be turned in) Palindrome is a string that is the same as backwards or forwards "otto "ababa" noon "ababbbbaba" In this lab we will work with strings (so we will be using character arrays) 1. Write a program that reads two strings str1 and str2 from the keyboard 2. Print both strings 3. Write a function computeLength and call this function in main to compute the length of the string strl and str2 4. if the string strl is of length 1 the program will terminate 5. if the string str2 is of length 0 the program will terminate Next A prefix of a string is a nonempty string that is an initial segment Example "moondog, has prefixes of “moo", “m", “moon", “mo", “moond",…. 6. Write a function isPrefix (str1, str2) that returns 1 if the string str2 is a prefix of strl substring of strl then isPrefix should return a ONE , if not return a ZERO See class discussion for the definition 7. Call this function with strl and str2 in main and output your result 8. Now write a function isPalindrome(str) that returns a 1 if str is a palindrome and 0 otherwise. To get full credit use isPrefix in your function 9. Call your function isPalindrome(strl) 10. Output the correct conclusion either PALINDROME or NOT PALINDROME You are NOT to use any standard library functions except for LO functions

Explanation / Answer

#include <stdio.h>

int computeLength(char *str) {

int n = 0;

while(str[n++] != '');

return n;

}

int isPrefix(char *str1, char *str2) {

int l1 = computeLength(str1);

int l2 = computeLength(str2);

if(l2 > l1)

return 0;

int i = 0;

while(i < l2){

if(str1[i] != str2[i])

return 0; // not prefix

i++;

}

return 1; // prefix

}

int isPalindrome(char *str) {

int length = computeLength(str);

int i= 0, j=length-1;

while(i < j) {

if(str[i] != str[j]) // not palindrome

return 0;

i++;

j--;

}

return 1; // palindrome

}

int main() {

return 0;

}