1

My program is to read a string from user and reverse it. When I use gets in Visual studio and Dev C++ gives me the right output but Linux gives me a warning and doesn't take it. When I use fgets instead of gets in all of them, the IDE's print out the reverse strings in new lines instead of printing all the words in the same line. Length is also different between gets and fgets.

#include <stdio.h>
#include <string.h>
void reverse(char *str, int i, int j); //function prototype
int main()
{
char str[100];
int start = 0;
int i = 0, len;

printf("Enter a string: \n");//fire work
gets(str);//fgets(str,100,stdin) //strtok(str,"\n")->outouts garbage 
len = strlen(str);
printf("length %d\n",len);
while (i < len) //we find the position of all the words and reverse them using the function
{
    printf("i=%d\n", i);
    if (*(str + i) == ' ')
    {
        reverse(str, start, i - 1);
        while (i < len && *(str + i) == ' ')
            i++;
        start = i;
    }
    i++;
}

reverse(str, start, len - 1);
printf("%s\n",str); //printing the reversed string

return 0;
}
void reverse(char *str, int i, int j) //function to reverse str string in 
str given range
{
char ch;
if (i >= j)
    return;

ch = *(str + i);
*(str + i) = *(str + j);
*(str + j) = ch;

reverse(str, ++i, --j);
}
//edit
My expected output should be 
length is 9.
erif krow
end of edit//

gets(str) gives -
Visual Studio and Dev C++: 
length is 9. 
erif krow.
MobaXterm(Linux)
warning for gets

fgets(str,100,stdin) gives for all IDEs -
length is 10.
erif
krow
serene
  • 19
  • 3
  • 1
    `gets` is deprecated and you shouldn't use it. `fgets` leaves the line ending at the end of the line, so if you don't want it you need to remove it. – Retired Ninja Apr 02 '19 at 00:53
  • 5
    That's because `fgets` retains the trailing newline. Notice how reversing "work" to "krow" also repositioned the newline. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) and also read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Weather Vane Apr 02 '19 at 00:54
  • @RetiredNinja I need the output to be printed on the same line. Fgets is just printing the second part on a newline. – serene Apr 02 '19 at 03:38
  • @WeatherVane the answers are too complicated. I honestly don't understand how to implement those answers on my code. I am not reading from any file. All I need is to have the output print on the same line. I feel so frustrated. – serene Apr 02 '19 at 03:52
  • 1
    **There is no gets function**. The old books that tell you about its existence are obsolete. – n. m. could be an AI Apr 02 '19 at 03:58
  • 1
    Add `str[strcspn(str, "\n")] = 0;` to your code right after `fgets`. – Retired Ninja Apr 02 '19 at 04:03
  • what 'answers' are you referring to? as when I write this comment, there are NO answers – user3629249 Apr 02 '19 at 04:23
  • OT: for ease of readability and understanding: 1) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces – user3629249 Apr 02 '19 at 04:26
  • regarding: `if (*(str + i) == ' ') { reverse(str, start, i - 1);` if the first character in the input is a space, then this will crash – user3629249 Apr 02 '19 at 04:35
  • regarding: *All I need is to have the output print on the same line.* The line of input from the user was terminated via newline, so the cursor is ALREADY at the beginning of a new line. Unless you know how to use the ANSI escape sequences (or something similar), you cannot move the cursor back to the original line, after the end of the original input. If your talking about a newline in the middle of the output, simply replace the newline with a '\0' before starting to process the line. Suggest using `strspn()` to perform that detail – user3629249 Apr 02 '19 at 04:44

0 Answers0