关于#c语言#的问题:c语言的逆序输出报错

问题

我想写一个c语言的逆序输出字符,
输入:qwe
输出:ewq
下面是我写的代码,没有报错,也不逆序输出,我看不懂我这问题出在了哪里,在线求解!

#include 
#include 
void main(){
    char ch,c[100];
    int i=0;
    while(ch=getchar()!='\n'){
        c[i]=ch;
        i++;
    }
    for(int j=i;j>0;j--){
        putchar(c[j]);
    }
}

首先,在循环条件中,你使用了一个赋值运算符,而不是比较运算符。这意味着条件始终为真,因此程序将无限循环。

正确的条件应该是:

while((ch=getchar()) != '\n'){
c[i]=ch;
i++;
}

另外,在输出字符串的部分,你使用了一个错误的循环范围,应该是

for(int j=i-1;j>=0;j--){
putchar(c[j]);
}

这样就可以正确地输入和输出字符串了。

putchar带缓冲


#include <stdio.h>
#include <string.h>
void main(){
    char c[100];
    int i,j,t,n;
    gets(c);
    n=strlen(c);
    for(i=0;i<=n/2;i++){
        t=c[i];
        c[i]=c[n-1-i];
        c[n-1-i]=t;
    }
    for(j=0;j<n;j++){
        printf("%c",c[j]);
    }
}