gpt4 book ai didi

c - 试图反转给定的单词

转载 作者:太空宇宙 更新时间:2023-11-04 06:58:43 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>

int main()
{
char s[100],temp1[100];
int i;

printf("Some word ");
scanf("%s", s);

for (i = 0; s[i] != '\0'; ++i);


int a = i-1;
for (int k = 0; k < i; k++) {

temp1[k] = s[a];
a--;
}

printf("%s", temp1);


system("pause");
}

我正在尝试反转给定的单词,但我的答案只是得到了一些其他的东西。我知道我只能搜索它,但我想知道为什么我会那样失败,所以请帮助我。

最佳答案

您需要null 终止结果。添加

temp1[i] = '\0';

此外,将 scanf() 更改为

scanf("99%s", s);

关于c - 试图反转给定的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41000436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com