My goal is to take a line from user, reverse the order of words and print it back.
Now the logic of the code is ok and works perfectly fine, but the problem is that I read the line using fgets and fgets automatically puts an '\n' , I tried changing it to '' but I received an error, What do you suggest I do except for the easy way with a for loop that prints the string from index 1 instead of zero?
More precisely is there a way of slicing the string as we do in python very easily?
For example:Input = I wanna ask a question
Expected output = question a ask wanna I
The output I get because the \n is replaced with \0 : question
Asked
Active
Viewed 26 times
0

Alireza Mirzaei
- 23
- 7
-
2Per the linked duplicate: `buffer[strcspn(buffer, "\n")] = 0;` – Andrew Henle Dec 17 '20 at 19:37
-
@AndrewHenle I receive the error identifier "buffer" is undefined, is there(except for stdio.h and string.h) any header I should include? – Alireza Mirzaei Dec 18 '20 at 10:31
-
You mustn't copy&paste code from an answer without adapting to your code. If your array has a different name, use that name. This applies to every answer and example you will find on any webseite. They are not tailored for your code. – Gerhardh Dec 18 '20 at 11:06
-
@Gerhardh I thought buffer was a predefined function or something similar, and now i feel stupid, thanks for the heads up. – Alireza Mirzaei Dec 18 '20 at 11:44
-
1@AlirezaMirzaei *now i feel stupid* That's an undocumented feature of C - the language ***will*** let you know how stupid you really are. ;-) – Andrew Henle Dec 18 '20 at 12:00