首页 > 解决方案 > 如何存储循环中的数组输出而不打印它

问题描述

我试图解决网站上的编程问题。它说检查这个词是否是回文。如果是,则打印“是”,如果不是,则打印“否”。我已经完成了,但有一个问题。我无法存储数组反转字符串的输出。

我尝试了很多方法。但我失败了。

#include<stdio.h>
#include<string.h>

int main(){

    int i,len;
    char mainword[100], reverseword[100];

    scanf("%s",mainword);

    len = strlen(mainword);

    strcpy(reverseword,mainword);

    for(i=len; i>=0; i--){
        printf("%c",reverseword[i]);
              // I just need here to save the output without printing it. So, that later I can compare it. 

    }

    if(strcmp(reverseword,mainword)==0){
        printf("\nYes");
    }
    else{
        printf("\nNo");
    }
}

我希望它将存储字符串值。

标签: c

解决方案


你可以试试这个:

#include<stdio.h>
#include<string.h>

int main(){

    int i,len,j=0;
    char mainword[100], reverseword[100];

    scanf("%s",mainword);

    len = strlen(mainword);

    for(i=len; i>=0; i--){
        reverseword[j] = mainword[i-1];
        j++;
    }

    reverseword[j] = '\0';

    if(strcmp(reverseword,mainword)==0){
        printf("\nYes");
    }
    else{
        printf("\nNo");
    }
}

推荐阅读