求助
用数组判断是否是回文字符串 #include <string.h>#include <stdio.h>
#include <stdbool.h>
void solve(char data[]){
bool flag = true;
int len = strlen(data);
for(int i = 0; i < len/2; ++i){
if(data != data){
flag = false;
break;
}
}
if(flag){
printf("%s是回文串",data);
}else printf("%s不是回文串",data);
}
int main(void){
char data;
printf("请输入字符串:");
gets(data);
solve(data);
return 0;
} baige 发表于 2020-11-15 21:45
能只用一个stdio.h吗?其他两种没学,不允许用,麻烦了,谢谢 #include <stdio.h>
#include <assert.h>
int huiwen(char *p)
{
char *q;
assert(*p != NULL);
q = p;
while (*p != '\0')
{
p++;
}
p--;
while (*q != '\0')
{
if (*p == *q)
{
p--;
q++;
}
else
return -1;
}
return 1;
}
int main()
{
int result;
char p[] = "abcdefgfedcba";
result = huiwen(p);
if (result == 1)
printf("Yes");
else
printf("No");
printf("\n");
return 0;
} 能只用一个stdio.h吗?另一种没学,不允许用,麻烦了,谢谢
#include <stdio.h>
int length(char data[])
{
int ans = 0, i = 0;
while(data!='\0')++ans;
return ans;
}
void solve(char data[]){
int flag = 1;
int len = length(data);
for(int i = 0; i < len/2; ++i){
if(data != data){
flag = 0;
break;
}
}
if(flag){
printf("%s是回文串",data);
}else printf("%s不是回文串",data);
}
int main(void){
char data;
printf("请输入字符串:");
gets(data);
solve(data);
return 0;
} baige 发表于 2020-11-15 22:09
谢谢
页:
[1]