倒影Gaara 发表于 2017-12-12 11:28:05

删除前后空格 并判断是否为回文

我不知道哪里错了输不出来

#include<stdio.h>
#include<string.h>
int main ()
{
        char s,ch,*p,*q;
        int n;
        gets(s);
        p=s;
    while(*p==' ')
    *p=*p++;
    n=strlen(s);
    q+=n-1;
    while(*q==' ')
    *q=*q--;
    while(p<q&&*p==*q)
    {
            p++;
            q--;
        }
    if(p<q)
    printf("no\n");
    else
    printf("yes");
}

BngThea 发表于 2017-12-12 11:33:40

q没有初始化,怎么可以执行
q+=n-1;

另外gets函数并不靠谱,建议使用fgets函数

倒影Gaara 发表于 2017-12-12 11:36:19

BngThea 发表于 2017-12-12 11:33
q没有初始化,怎么可以执行
q+=n-1;



那应该怎么初始化q?
我们刚接触指针

BngThea 发表于 2017-12-12 11:39:32

倒影Gaara 发表于 2017-12-12 11:36
那应该怎么初始化q?
我们刚接触指针

根据你的意思,你应该将它也指向s

倒影Gaara 发表于 2017-12-12 11:44:34

BngThea 发表于 2017-12-12 11:39
根据你的意思,你应该将它也指向s

题目是到填空题
#include<stdio.h>
#include<string.h>
int main ()
{
&nbsp; &nbsp; &nbsp; &nbsp; char s,ch,*p,*q;
&nbsp; &nbsp; &nbsp; &nbsp; int n;
&nbsp; &nbsp; &nbsp; &nbsp; gets(s);
&nbsp; &nbsp; &nbsp; &nbsp; p=s;
&nbsp; &nbsp; while(*p==' ')
&nbsp; &nbsp; *p=——;
&nbsp; &nbsp; n=strlen(s);
&nbsp; &nbsp; q=——;
&nbsp; &nbsp; while(*q==' ')
&nbsp; &nbsp; ——;
&nbsp; &nbsp; while(——&&*p==*q)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p++;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ——;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; if(p<q)
&nbsp; &nbsp; printf("no\n");
&nbsp; &nbsp; else
&nbsp; &nbsp; printf("yes");
}

倒影Gaara 发表于 2017-12-12 11:50:33

#include<stdio.h>
#include<string.h>
int main ()
{
&nbsp; &nbsp; &nbsp; &nbsp; char s,ch,*p,*q;
&nbsp; &nbsp; &nbsp; &nbsp; int n;
&nbsp; &nbsp; &nbsp; &nbsp; gets(s);
&nbsp; &nbsp; &nbsp; &nbsp; p=s;
&nbsp; &nbsp; while(*p==' ')
&nbsp; &nbsp; ——;
&nbsp; &nbsp; n=strlen(s);
&nbsp; &nbsp; q=——;
&nbsp; &nbsp; while(*q==' ')
&nbsp; &nbsp; ——;
&nbsp; &nbsp; while(——&&*p==*q)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p++;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ——;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; if(p<q)
&nbsp; &nbsp; printf("no\n");
&nbsp; &nbsp; else
&nbsp; &nbsp; printf("yes");
}

BngThea 发表于 2017-12-12 12:22:09

倒影Gaara 发表于 2017-12-12 11:44
题目是到填空题
#include
#include


那就改成 q = s + n - 1

倒影Gaara 发表于 2017-12-12 12:26:57

BngThea 发表于 2017-12-12 12:22
那就改成 q = s + n - 1

还是输不出来请问我删除前后空格那里是不是对的啊 ?
页: [1]
查看完整版本: 删除前后空格 并判断是否为回文