0

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. 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.

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]);

    } 
}
Eduardo Pascual Aseff
  • 1,149
  • 2
  • 13
  • 26
Jones
  • 1
  • 1
    Does this answer your question? [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – Adrian Mole Apr 06 '20 at 19:51
  • 2
    Explicitly calling `main` is dodgy at best and probably will cause undefined behaviour on many platforms. – Adrian Mole Apr 06 '20 at 19:53
  • What extra line? On input `foo\n`, your program writes out `\noof`, as expected. – William Pursell Apr 06 '20 at 19:56

2 Answers2

0

Look at this if statement

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'.

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.

You can remove the character from the array before passing it to the function the following way

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

Or it is evven to do this before the if statement and the if statement rewrite like

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.

return main();

You could enclose the code in main in a loop.

The function itself should be declared and defined at least the following way

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

!dlroW olleH
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

It doesn't have anything to do with the null character at the end of the string. 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]".

The second newline occurs because fgets() returns the string including the newline, like "abc\n". Your revStr() function then prints the string in reverse order with the newline first.

Ben Cass
  • 133
  • 2
  • 7