C Programming. Problem 1. Palindromes A palindrome is a word or a phrase that re
ID: 3776802 • Letter: C
Question
C Programming. Problem 1. Palindromes
A palindrome is a word or a phrase that reads the same backward as forward, e.g., "madam" or "nurses run", or "never odd or even". For the purpose of this assignment we will ignore whitespace characters, such as blank characters' 'tabs ' ' comma ', ' and newlines ' ' Write in a file p1.c a function called is Palindrome() that takes a string as parameter and returns TRUE (1) if the string is a palindrome or FALSE (0) otherwise. It is important to remember to ignore (skip) all whitespace characters, so that isPalindrome("never odd or even") should return TRUE. The original string without whitespaces is "never odd or even" and it follows the definition of a palindrome. Write the function contract before the function. Assume the following: the longest string passed as parameter has maximum 100 characters, including the '' at the end. you should ignore whitespace and you can use function int isspace(int c) from header ctype.h to check if a character c is whitespace. that strings are case sensitive - so there is no need to convert the string to the same case. Write in file p1.c a main() function that reads from the terminal in a loop a text line using the fgets() function and then uses the isPalindrome() function to display whether the input text is a palindrome or not. If the string is not a palindrome, the program displays "not a palindrome". If the string is a palindrome, the program displays the palindrome and then "is a palindrome". The loop should end at the end-of-file (user types CTRL-Z or CTRL-D) OR when the user enters the word "quit". To read one line of text (with max. TEXTSIZE - 1 characters) declared as char line [TEXTSIZE]; from the terminal with function fgets(), use the function call: fgets(stdin, TEXTSIZE, line) Remember, stdin is a FILE* corresponding to the terminal, and fgets() stores the ' ' char at the end of the string, before the '' terminator. fgets() returns NULL when it encounters EOF. Here is a sample user session. User input is highlighted with yellow. Too much not a palindrome turkey and gravy not a palindrome rise to vote, sir risetovotesir is a palindrone never odd or even neveroddoreven is a palindrome quit (user enters "quit" or types CTRL-Z (EOF on Windows) to finish the program) Ensure the program compiles correctly and works correctly in Visual Studio. Test a few times Create a new MS Word file called homevyork4.doc. Write your name at the beginning of this file. Write a heading Problem 1 Copy-paste the content of the p1.c file to file homework4.doc. Take a screenshot of the program after it completed, showing at least 3 cases of palindromes and 3 cases with non-palindromes, and with all the output displayed. Do NOT use the same input in the same order as given above. Make sure the cmd.exe window is visible on top of Visual Studio, as we did before. Insert/paste the screenshot into the homework4.doc file.Explanation / Answer
Answer:
#include <stdio.h>
#include <string.h>
void main()
{
char input[25], reverse_input[25] = {''};
int i, length = 0, set = 0;
printf("Enter a input ");
gets(input);
for (i = length - 1; i >= 0 ; i--)
{
reverse_input[length - i - 1] = input[i];
}
for (set = 1, i = 0; i < length ; i++)
{
if (reverse_input[i] != input[i])
set = 0;
}
if (set == 1)
printf ("%s is a palindrome ", input);
else
printf("%s is not a palindrome ", input);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.