0

The problem is that when I try to reverse a word, there are some words that prints out with unspecified and random characters. I tried to change the string size and the results change (some words make up random characters and some no)

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

#define LEN 20

void reverse( char w[LEN]);

int main() {
char word[LEN];

reverse(word);

return 0;
}

void reverse (char w[LEN]){

char rev[LEN];
int i,j;
fgets(w, LEN, stdin);

for (i = 0, j = strlen(w)-1; i < strlen(w); i++, j--){
    rev[j] = w[i]; 
}

printf("%s\n", rev);

return;

}

An example of my problem is this:

Input: bathroom

Output: moorhtabp⬧⬧oz⬧

Every time I execute the program, with the same input the output changes.

wovano
  • 4,543
  • 5
  • 22
  • 49
reshi
  • 1

3 Answers3

1

You simply need to add a '\0' or 0 at the end of rev to terminate it.

for (i = 0, j = strlen(w)-1; i < strlen(w); i++, j--)
{
    rev[j] = w[i]; 
}
rev[i] = 0;
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
1

There are a few errors:

  1. fgets() add an newline \n at the end of the string.
  2. You must add a string terminator (0 byte) after the last character.
  3. (Lesser issue) Using strlen() in the loop condition will iterate over the string multiple times. It usually results in O(N^2) algorithms.

Fixes:

  1. Variant A

Use scanf to parse a single word scanf(" %19s", w) The specifier %19s scans up to 19 characters and add NULL terminator to fix 20-char-long array

  1. Variant B

Check if the last character of w is \n. If so, replace it with zero.

int L = strlen(w);
if (L > 0 && w[L - 1] == '\n') {
  w[--L] = 0;
}
  1. Just put
rev[i] = 0;

at the end.

  1. Just iterate until j is negative, meaning it reached the "one before first" character and there is nothing more to do.
for (i = 0, j = L-1; j >= 0; i++, j--)
    rev[j] = w[i]; 
tstanisl
  • 13,520
  • 2
  • 25
  • 40
1

For starters the call of the function

char word[LEN];

reverse(word);

does not make a sense because neither string is passed to the function. You passed an uninitialized character array.

The function should do only one thing: reverse the passed string. So the call of fgets should be removed from the function and placed in main.

The function fgets can append the new line character '\n' to the entered string. You need to remove it.

Within the function reverse you forgot to append the terminating zero character '\0' to the reversed string. So this call

printf("%s\n", rev);

invokes undefined behavior.

Also the type of the returned value of the standard C function strlen is the unsigned integer type size_t. You should use this type instaed of the signed integer type int.

And this declaration of the function

void reverse( char w[LEN])

also does not make a great sense because the compiler will adjust it to the following declaration

void reverse( char *w)

and in general the user can pass to the function a string of any length that can be greater than the value LEN.

Below there is a demonstrative program that shows how the function can be written.

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

char * reverse( char *s )
{
    for ( size_t i = 0, n = strlen( s ); i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n - i - 1];
        s[n - i - 1] = c;
    }
    
    return s;
}

int main(void) 
{
    enum { N = 20 };
    char s[N];
    
    fgets( s, N, stdin );
    
    s[ strcspn( s, "\n" ) ] = '\0';
    
    puts( s );
    puts( reverse( s ) );
    
    return 0;
}

If to enter string "Hello World!" then the program output will be

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