如下代码,GCC无法编译:

#include <stdio.h>
#include <string.h>
int main()
{
    char str[200],ch;
    char *p,*q;
    gets(str);
    //fgets(str,200,stdin);
    p=str;
    q=p+strlen(p) -1;
    while(p<q)
    {
        ch=*p;
        *p++=*q;
        *q--=ch;
    }
    printf("%s\n",str);
}
error: ‘gets’ was not declared in this scope
     gets (str);
     ^~~~

解决办法:

fgets(str,字符串最大长度n,stdin);

#include <stdio.h>
#include <string.h>
int main()
{
    char str[200],ch;
    char *p,*q;
    fgets(str,200,stdin);
    p=str;
    q=p+strlen(p) -1;
    while(p<q)
    {
        ch=*p;
        *p++=*q;
        *q--=ch;
    }
    printf("%s\n",str);
}
I am very shengqi

iqgnehs yrev ma I

 

Logo

更多推荐