c - 试图反转给定的单词

标签 c reversing

#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/

相关文章:

linux - 如何在Linux上打开端口以运行应用程序

c - 如何按值可变大小数组传递(到函数)?

LED驱动器与Arduino上的模数转换之间的冲突

c - 丢失数组指针值?

Perl - 源代码的操作码?

c++ - 反转哈希,查找冲突(与总和和左/右移位进行异或)

C程序来反转文件的内容并将其写入另一个文件

python - 字节反转 AB CD 到 CD AB 与 python

c++ - 指向(定义了指针数组)字符串的指针数组 : Are the strings stored sequential in memory?

c - 需要解释 - 3-D 数组、指针