C programming language: In this program, you will read an input string from stdi
ID: 3668038 • Letter: C
Question
C programming language:
In this program, you will read an input string from stdin, print it back to the user, and
report if it is a palindrome. The string ends when whitespace is encountered(Do not use string.h). A
palindrome is a string that is the same read forwards or backwards. Examples of
palindromes include :
abba
racecar
0912190
1. Assumptions you may make for the test cases (You do not need to verify this):
a. The input string is at most 30 characters.
b. The input string contains letters, digits, and punctuation characters only.
c. Assume the string has no spaces. Distinguish between uppercase and
(And the string ends with a null terminator.)
lowercase, so abba is a palindrome but AbBa is not.
2. Constraints:
a. Use scanf to read the string. The format string should use %s. Do not use
any other function to read input from stdin.
3. Libraries(Do not use string.h):: stdio.h &stdlib.h
[prompt]$./is_palindrome
Enterastringtotest:racecar
racecarisapalindrome
[prompt]$./is_palindrome
Enterastringtotest:09910
09910isnotapalindrome
Explanation / Answer
#include <stdio.h>
int main(){
char string1[30];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
for(i = 0; string1[i] !=''; i++){
length = length +1;
}
for(i=0;i <=length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.