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: 3727425 • 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

str

2

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

str1

and

str2

.

4.

if the string

str1

is of length 1 the program will terminate

5.

if the string

str

2

is of length 0 the program will ter

minate

Next

A

prefix

of a st

ring is a nonempty string that is an initial segment

Example

“moondog”

has prefixes of “moo”, “m”, “moon”, “mo”, “moond”,....

6.

Write a function

is

Prefix (str1, str2)

that returns 1 if the string

str

2

is a prefix of

str1 substring of

str1

then

is

Prefix

should return a ONE , if not retur

n a ZERO.

See class discussion for the definition

7.

Call this function with str1 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 i

sPrefix

i n your function

9.

Call your function

isPalindrome(str

1)

10.

O utput the correct conclusion either

PALINDROME

or

NOT PALINDROME

You are

NOT

to use any standard library functions except for I/O functions

.

You will determine the above and output each in a form

atted manner.

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

int computeLength(char *str

);

int is

Reverse

Prefix(char *str1, char *str2);

int isPalindrome

(char *str

);

Explanation / Answer

int computeLength(char *str) {

int n = 0;

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

return n;

}

int isReversePrefix(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

}