你把这说清楚了我再补充回答你~
我写了个参考代码给你参考, 首先是输入你要判断的字符串的个数, 然后再依次输入所有的字符串, 最后判断输入的所有字符串是否是"回文"! 因为不理解你那句话, 所以暂时没做什么空和什么"#"处理.
详细c代码:
#include <stdio.h>
#include <string.h>
#define STR_LEN 128
#define STR_NUM 64
int main()
{
int i = 0, j = 0, n = 0
int len = 0
char *start = NULL
char *end = NULL
char str[STR_NUM][STR_LEN] = {0}
printf("Please input the number of string: \n")
scanf("%d", &n)
printf("Please input all the string: \n")
for (i = 0i <ni++)
{
scanf("%s", str[i])
}
for (j = 0j <nj++)
{
start = str[j]
len = strlen(str[j])
end = str[j] + len - 1
while (start - end <= 0)
{
if (*start++ != *end--)
{
break
}
}
if (start >end)
{
printf("yes\n")
}
else
{
printf("no\n")
}
}
return 0
}
例子,运行后:
Please input the number of string:
4
Please input all the string:
aba
112ds
madam
xyzyx
yes
no
yes
yes
Press any key to continue
补充回答:
大概明白你的意思,我会把例子贴上, 若不符合你的要求我再帮修改!
详细代码如下:
#include <stdio.h>
#include <string.h>
#define STR_LEN 128
#define STR_NUM 64
int main()
{
int i = 0
int len = 0
int str_count = 0
char *start = NULL
char *end = NULL
char str[STR_NUM][STR_LEN] = {0}
printf("Please input all the string: \n")
while (1)
{
scanf("%s", str[str_count])
if (str[str_count][0] == '#')
{
break
}
else
{
str_count++
continue
}
}
for (i = 0i <str_counti++)
{
start = str[i]
len = strlen(str[i])
end = str[i] + len - 1
while (start - end <= 0)
{
if (*start++ != *end--)
{
break
}
}
if (start >end)
{
printf("yes\n")
}
else
{
printf("no\n")
}
}
return 0
}
运行实例1:
Please input all the string:
xyzyx
adghf
#
yes
no
Press any key to continue
运行实例2:
Please input all the string:
1232ss
sakljfkla
333dafs
aba
ee3
xyzyx
dfj222
madam
111$111
slsl33
#
no
no
no
yes
no
yes
no
yes
yes
no
Press any key to continue
判断回文字符串的办法就是成对比较,就是把字符串的第一个字符和倒数第一个字符比较,把第二个字符和倒数第二个比较……,以此类推。所以要写一个判断字符串是否回文的函数要先算出字符串S的字符数n,然后把S[0]和S[n-1-0]比较,S[1]和S[n-1-1]比较,S[2]和S[n-1-2]比较,……,直到把S[n/2]和S[n-1-n/2]比较,只要发现一个不同就可以判断不是回文,否则就是回文。具体判断步骤如下。
1、进入VC++,新建C++文件。
2、编写程序:
#include <iostream> //控制台 *** 作头文件#include <string.h> //字符串 *** 作头文件 bool Hw(char s[]) //检测是否回文的函数{int i,n=strlen(s) //循环变量和字符串长度 if(n<=1) return false //空串或只有一个字符不算回文串 for(i=0i<n/2i++) //用循环逐个检测 if(s[i]!=s[n-1-i]) //如果对称位置的字符不一样 return false //结束函数并返回假 return true} //检测完还没发现不同就返回真 int main() //主函数{int i //循环变量 char S[255] //用于存放字符串的数组 for() //弄一个无穷循环 {printf("请输入字符串(END结束):") //显示提示信息 scanf("%s",S) //从键盘输入字符串到S中 if(strcmp(S,"END")==0) break //如果输入END退出循环 if(Hw(S)) //用自定义函数判断是否回文 printf("这是回文字符串\n\n") //显示判断结果 else printf("这不是回文字符串\n\n")} printf("\n") //输完所有数再空一行 system("PAUSE") //暂停屏幕以便查看显示结果 return 0} //结束程序3、检查没有错误进行编辑连接运行,运行结果如下。
#include<stdio.h>#include<string.h>
int strcmp_self(char *a)
{
int length=strlen(a)
int i,j
for(i=0,j=length-1i<length/2+1i++,j--)
{
if(a[i]!=a[j])
{
return 1
}
}
return 0
}
void main()
{
printf("请输入一个字符串:\n")
char s[80]
gets(s)
if(strcmp_self(s)==0)
printf("是回文")
else
printf("不是回文")
}
思路:从两端开始比较a[0]和a[length-1],a[1]和倒数第二个;只要出现了不相等的情况,就不是回文,至于i<length/2+1这个条件,就是比较到中间的那个数就可以结束了,改成i<length其实也可以,表示i从头到尾,j从尾到头,就是多了不必要的比较//记得采纳哦
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)