简体   繁体   English

为什么我的反转字符数组会打印额外的一行?

[英]Why does my reversed char array print an extra line?

I'm trying to get my code to except input and then reverse and print it without any extra lines.我试图让我的代码除了输入之外,然后在没有任何额外行的情况下反转并打印它。 I've taken all the \n out to see if that helps but I always get a new line before my print.我已经把所有的 \n 都拿出来看看是否有帮助,但我总是在打印之前换行。 I'm guessing that it's printing the null character at the end of the users input but I don't know how to get rid of it.我猜它在用户输入的末尾打印 null 字符,但我不知道如何摆脱它。

This is what I have so far这是我到目前为止所拥有的

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

void revStr(char str[]);

int main(void) 
{ 
    char str[50]; 
    fgets(str, 50, stdin);

    if ((strcmp(str, "quit\n") == 0) || (strcmp(str, "q\n")==0) || (strcmp(str, "Quit\n")== 0)){
        printf("\n");
        return 0;
    }
    else{
        revStr(str);
        return main();
    }  
} 
void revStr(char str[])
{
    int arrSz = strlen(str); 

    int i; 
    for(i = arrSz-1; i >= 0; i--){
        if(str[i] != '\0')
        printf("%c",str[i]);

    } 
}

Look at this if statement看看这个 if 语句

if ((strcmp(str, "quit\n") == 0) || (strcmp(str, "q\n")==0) || (strcmp(str, "Quit\n")== 0)){

You see that each string literal used in the condition has additionally the new line character '\n' .您会看到条件中使用的每个字符串文字都有新的换行符'\n'

It means that the character array str has this character and this character is outputted in the function that outputs the array in the reverse order.表示字符数组str有这个字符,这个字符在输出数组的function中以相反的顺序输出。

You can remove the character from the array before passing it to the function the following way您可以通过以下方式将字符从数组中删除,然后再将其传递给 function

str[ strcspn( str, "\n" ) ] = '\0';

Or it is evven to do this before the if statement and the if statement rewrite like或者甚至在 if 语句和 if 语句重写之前这样做

if ((strcmp(str, "quit") == 0) || (strcmp(str, "q")==0) || (strcmp(str, "Quit")== 0)){

Pay attention to that the recursive call of main does not make great sense.注意 main 的递归调用没有多大意义。

return main();

You could enclose the code in main in a loop.您可以将 main 中的代码包含在一个循环中。

The function itself should be declared and defined at least the following way function 本身应至少按以下方式声明和定义

void revStr( const char str[] )
{
    size_t arrSz = strlen( str ); 

    while ( arrSz-- ) putchar( str[arrSz] );
}

Here is a demonstrative program这是一个演示程序

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

void revStr( const char str[] )
{
    size_t arrSz = strlen( str ); 

    while ( arrSz-- ) putchar( str[arrSz] );
}

int main(void) 
{
    revStr( "Hello World!" );

    return 0;
}

Its output is它的 output 是

!dlroW olleH

It doesn't have anything to do with the null character at the end of the string.它与字符串末尾的 null 字符没有任何关系。 When using the program from the terminal, it appears to contain two newlines like:从终端使用程序时,它似乎包含两个换行符,例如:

abc

cba

The first newline appears because the console is printing everything you type "abc[enter]".出现第一个换行符是因为控制台正在打印您键入“abc[enter]”的所有内容。

The second newline occurs because fgets() returns the string including the newline, like "abc\n".出现第二个换行符是因为fgets()返回包含换行符的字符串,例如“abc\n”。 Your revStr() function then prints the string in reverse order with the newline first.您的revStr() function 然后以相反的顺序打印字符串,首先是换行符。

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

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