Using Loops, Design a program that checks whether a string is a palindrome. The
ID: 3753310 • Letter: U
Question
Using Loops, Design a program that checks whether a string is a palindrome.
The string can be several words or just a single word:
Acme Palindrome Finder Enter a string to check: Was it a rat I saw?
Analyzing input. Please wait……… Your input: Was it a rat I saw Reverse input: was I tar a ti saW
Congratulations! Palindrome found!
Minimum Requirements: • Your program should pick a random number from 3 to 7 and wait that many seconds before displaying the results. Use functions from the time.h library to get the current time. Use a loop to query the system clock until the desired number of seconds have passed. NOTE:You can use the wait() function code shown in class, or you can use a delay function of your choice. • The input should not be case sensitive. • The program should work with numbers as well as letters. White space is not part of a palindrome, as you can see in the above example. Your algorithm should remove white space to check for a palindrome, but the final display should simply reverse the original string (keeping the white space intact).
BONUS (5pts): Instead of simply printing the reverse of the input string, make your display routine print the reversed string with spaces in the same locations as the input string. Using the original example, the output would look like this instead: Your input: Was it a rat I saw Reverse input: was It a rat i saW
Explanation / Answer
Following is the code to check whether the string is palindrome or not:-
#include<stdio.h>
#include<string.h>
void reverse(char[],int);
int main()
{
char a[100];
int len,i=0;
printf("Please enter the string to check: ");
fgets(a,100,stdin);
for (int i=0; i<strlen(a); ++i)
{
if (a[i] == ' ')
{
a[i]='';
}
}
len = strlen(a);
reverse(a,len);
return 0;
}
void reverse(char b[100],int n)
{
char c[100];
int k=0;
for (int i = n-1;i>=0;i--)
{
c[k] +=b[i];
k++;
}
c[k] = '';
if(strcmp(c,b) == 0)
printf("The Entered string is palindrome!n");
else
printf("The Entered string is not palindrome! ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.