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

Problem 6 (25 points) Your goal in this problem is to write a C program, called

ID: 3732204 • Letter: P

Question

Problem 6 (25 points) Your goal in this problem is to write a C program, called palindromes.c, that determines whether a string provided by the user is a palindrome. Here is what Wikipedia says about palindromes A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers. Famous examples include "A man, a plan, a canal, Pan- ama!", "Amor, Roma", “race car" and ..No ‘x' in Nixon". In this problem, we will indeed make allowances for punctuation, word dividers (such as blanks), and any other characters in the input string which are not lower-case letters a-z or upper-case letters A-Z Such characters should be stripped from the input string prior to deciding whether it is a palindrome. Moreover, upper-case and lower-case letters in the input string should be considered same. Thus the given names Anna, Bob, and Aviva are all palindromes. On the other hand, the empty string"" as well as strings that do not contain any letters at all are not palindromes Your program should first prompt the user to input a string. You can assume that this string will contain at most 80 characters. However, since the user string may contain blanks, it cannot be read using scanf ("%s", string). Instead, it should be read character-by-character until the newline character , is encountered. You will be asked to write a function read-string for this pur pose. Next, the input string should be processed to remove all the non-letter characters and convert upper-case letters A-Z to lower-case letters a-z. In order to implement this, you will be asked to write the function process_string. Finally, the processed string should be analyzed using the function is-palindrome. The function is_palindrome builds a string which is the reverse of the original string, using a call to the function reverse string. The original string is a palin- drome if and only if it is equal to its reverse. Your program should report to the user whether the input string is a palindrome or not, and terminate. Here are a few sample runs of this program /home/userXYZ/ECE15/Final> palindromes Enter a string: A man, a plan, a canal, Panama! Your string is a palindrome /home/userXYZ/ECE15/Final> palindromes Enter a string: Never odd or even Your string is a palindrome /home/userXYZ/ECE15/Final> palindromes Enter a string: 1234321 Your string is NOT a palindrome. Provided on the next page is part of the source code that implements the program palindromes.c. This part contains the forward function declarations, and the complete implementation of the function main).Your task is to implement the other functions. Note that in this problem, you are not allovw ed to use any functions from the C standard library, except for those declared in . In particular, you are not allowed to use the string manipulation functions declared in

Explanation / Answer

a)

int string_length(char* string)
{
int i;
int c1=0;//counter to count numbers of character in string
for(i=0;string[i]!='';i++)
{
c1++;
}
return c1;
}

b)

int compare_string(char *string1,char *string2)
{
int i,j;
int flag=0;
for(i=0; (string1[i]!='')||(string2[i]!=''); i++)
{
if(string1[i] != string2[i])
{
flag = 1;
break;
}
}
return flag;
}

c)

int read_string(char *string)
{
int i = 0;
int ch;
while((ch = getchar()) != ' ' && ch != EOF )
{
string[i++] = ch;
return ch;
}
string[i] = '';
return 0;
}

d)

void copy_string(char *string1,char *string2)
{
int i;
char s1[1000];
for(i=0; (string1[i]!=''); i++)
{
string2[i] = string1[i];
}
string2[i]='';
  printf("copied string = %s", string2);
}

void reverse_string(char *string)
{
int i,len,revIndex,stringIndex;
i = 0;
char reverse[1000];
while(string[i] != '')
{
i++;
}
len = i;
revIndex = 0;
stringIndex = len - 1;
while(stringIndex >= 0)
{
reverse[revIndex] = string[stringIndex];

stringIndex--;
revIndex++;
}
reverse[revIndex] = '';
printf("Reverse string = %s", reverse);
}

e)

void process_string(char *string)

{

int i,j;

for(i = 0; string[i] != ''; ++i)

{

while (!( (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z') || string[i] == '') )

{

for(j = i; string[j] != ''; ++j)

{

string[j] = string[j+1];

}

string[j] = '';

}

}

for(i=0;string[i]!='';i++)

{

if(string[i]>=97 && string[i]<=122)

{

string[i]=string[i]-32;

}

}

printf("%s",string);

}

f)

int is_palindrom(char *string)
{
int i,c1=0,flag=0;
for(i = 0; string[i] != ''; ++i)
{
c1++;
}
for(i = 0; i<c1; ++i)
{
if(string[i] != string[c1-i-1]){
flag = 1;
break;
}
}
return flag;
}