c - 如何存储循环中的数组输出而不打印它
问题描述
我试图解决网站上的编程问题。它说检查这个词是否是回文。如果是,则打印“是”,如果不是,则打印“否”。我已经完成了,但有一个问题。我无法存储数组反转字符串的输出。
我尝试了很多方法。但我失败了。
#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");
}
}
我希望它将存储字符串值。
解决方案
你可以试试这个:
#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");
}
}
推荐阅读
- c++ - Makefile 忽略特定行
- c# - 从工具条中打开表单的代码,但一个代码中的相同代码不能执行“.show();”
- reactjs - 反应 redux,thunk 完成事件过早发生
- css - 背景图像不会缩放以适合页面
- android - 一旦打开片段,ViewModelProvider 就会导致崩溃。我该如何解决这个问题?
- html - VS Code 中的红色标题标签错误 - HTML/CSS
- javascript - 如果执行了多个(AND 运算符)函数,则执行某些操作
- python - mysql和python中的多线程
- node.js - NodeJS在登录后重定向用户时在url中发送一个令牌
- javascript - 无法使用 chrome-cookies-secure 模块解析 Node.js 应用程序中的 Promise