Write a program that prompts the user to enter a string and reports whether the string is a palindrome. A string is a palindrome if it reads the same forward and backward. The words “mom,” “dad,” and “noon,” for instance, are all palindromes. You are not allowed to use the , and functions defined in the string.h library. You have to implement all the details by yourself. One solution is to check whether the first character in the string is the same as the last character. If so, check whether the second character is the same as the second-to-last character. This process continues until a mismatch is found or all the characters in the string are checked, except for the middle character if the string has an odd number of characters. strlen() strcmp() strrev() Here are two sample runs. --------------------------------------------------- Enter a string : mum The string "mum" is a panlindrome. --------------------------------------------------- Enter a string : Welcome The string "Welcome" is not a panlindrome. ---------------------------------------------------
时间: 2023-08-15 21:21:08 浏览: 104
Sure, I can help you with that! Here's the program in C:
```
#include <stdio.h>
int main() {
char str[100];
int i, j, len, flag = 1;
printf("Enter a string: ");
scanf("%s", str);
for (len = 0; str[len] != '\0'; len++);
for (i = 0, j = len - 1; i < len / 2; i++, j--) {
if (str[i] != str[j]) {
flag = 0;
break;
}
}
if (flag) {
printf("The string \"%s\" is a palindrome.\n", str);
} else {
printf("The string \"%s\" is not a palindrome.\n", str);
}
return 0;
}
```
The program prompts the user to enter a string using `printf()` and reads it in using `scanf()`. It then calculates the length of the string using a `for` loop that terminates when it reaches the null character `\0`.
The program then checks whether the string is a palindrome using another `for` loop that compares the first character with the last character, the second character with the second-to-last character, and so on. If a mismatch is found, the flag variable is set to 0 and the loop breaks. Finally, the program prints whether the string is a palindrome or not using `printf()`.
You'll notice that I didn't use any of the functions defined in the `string.h` library, as per your requirement. Instead, I implemented all the details of the program myself, as you requested.
阅读全文