问题:
编程判断输入的一个字符串是否是“回文”。所谓“回文”字符串就是左读和右读都一样的字符串。例如: "abcba"就是一个回文字符串。
输入提示信息:“Input a string:\n”
输入格式:gets()
判断是回文的输出提示信息:“This string is a plalindrome.”
判断不是回文的输出提示信息:“This string is not a plalindrome.”
程序运行示例1:
Input a string:
abcba↙
This string is a plalindrome.
程序运行示例2:
Input a string:
friend↙
This string is not a plalindrome.
#include <stdio.h>
#include <string.h>
#define N 100
int main()
{
char s[N];
int i,j,n,count=0;
printf("Input a string:\n");
gets(s);
n=strlen(s);
for(i=0,j=n-1;i<j;i++,j--)
{
if(s[i] == s[j])
count++;
else
count--;
}
if(count == (n/2))
printf("This string is a plalindrome.");
else
printf("This string is not a plalindrome.");
return 0;
}
本文介绍了一个简单的C语言程序,用于判断用户输入的字符串是否为回文。回文是指从左到右读和从右到左读都一样的字符串,如abcba。程序通过比较字符串的首尾字符来实现判断。
6万+





