简体   繁体   English

反转输入字符串

[英]Reversing an input string

I am trying to capture a user input string, then display that string in reverse order next to the initial string. 我试图捕获用户输入的字符串,然后以相反的顺序在初始字符串旁边显示该字符串。 My code is as follows: 我的代码如下:

char str[300], revstring[300];
int i, strlen;



int main(void) {


    printf("Enter a string: ");                 //Prompt user for input string
    gets(str);

    for (i = 0; str[i] != NULL; i++) {          //Get length of string
        strlen += 1;
    }

    for (i = 0; i <= strlen; i++) {
        revstring[i] = str[strlen - i];
    }

    printf("\n\nThe palindrome of your input is %s%s\n\n\n", str, revstring);

    return 0;
}

When I run the program however, I see nothing after the initial string. 但是,当我运行程序时,在初始字符串之后看不到任何内容。 I come from a python background so maybe I am thinking about this in too much of a python mindset, but I feel like this should work. 我来自python背景,所以也许我是从python的思维定势中考虑这个问题的,但是我觉得这应该可行。

The string is a null-terminated string. 该字符串是一个以空字符结尾的字符串。 You are copying the null character to the beginning of the reversed string. 您正在将空字符复制到反向字符串的开头。 This tells the system that they string is terminated at the first character. 这告诉系统它们的字符串在第一个字符处终止。

You could use this code instead. 您可以改用此代码。

for (i = 0; i < strlen; i++)
{
    revstring[i] = str[(strlen - 1) - i];
}
revstring[strlen] = 0;

Here only the characters before the null character are copied and then the null character is added at the end. 在这里,仅复制空字符之前的字符,然后在末尾添加空字符。

After this loop 在此循环之后

for (i = 0; str[i] != NULL; i++) {          //Get length of string
    strlen += 1;
}

str[strlen] is equal to the terminating zero '\\0' . str[strlen]等于终止零'\\0' And the next loop starts from writing this zero in the first element of the array revstring when i is equal to 0. i等于0时,下一个循环从在数组revstring的第一个元素中写入零开始。

for (i = 0; i <= strlen; i++) {
    revstring[i] = str[strlen - i];
}

As result nothing is displayed. 结果什么也没显示。

Also you should not forget to append the result string with the terminating zero. 同样,您也不要忘记在结果字符串后附加零。

Take into account that the function gets is unsafe and is not supported any more by the C Standard. 考虑到该函数gets是不安全的,并且C标准不再支持该函数。 It is better to use the standard function fgets . 最好使用标准函数fgets But using it you should remove the appended new line character. 但是使用它应该删除附加的换行符。

The program can be written the 该程序可以写成

#include <stdio.h>

#define N   300

int main( void ) 
{
    char str[N], revstring[N];

    printf( "Enter a string: " );

    fgets( str, N, stdin );

    size_t  length = 0;
    while ( str[length] != '\0' && str[length] != '\n' ) ++length;

    if ( str[length] == '\n' ) str[length] = '\0';

    size_t i = 0;
    for ( ; i != length; i++ ) revstring[i] = str[length - i - 1];

    revstring[i] = '\0';

    printf("\n\nThe palindrome of your input is %s%s\n\n\n", str, revstring);

    return 0;
}

Its output might look like 它的输出可能看起来像

Enter a string: Hello, Froobyflake

The palindrome of your input is Hello, FroobyflakeekalfyboorF ,olleH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM