0

My code looks some like below

printf("string:");
fgets(s,58,stdin);

prinf("xyz %s abc", s);

The problem I face is the "abc" goes to the next line

E.g. string is kkkk

Then output is

xyz kkkk
 abc

like even the space goes in the next line so how to fix this so all xyz string and abc are in same line?

I want to do this using fgets not scanf.

chux - Reinstate Monica
  • 127,356
  • 13
  • 118
  • 231
Noob
  • 1
  • The input you gave to `fgets()` was 5 characters: `"kkkk\n"` - agreed? The output included those 5 characters. "fix this so all xyz string and abc are in same line?" --> 2 choices: do not print the `'\n'` that was read (easy) or do not include a end-of-line in the input (hard). – chux - Reinstate Monica Mar 11 '22 at 13:24
  • If i don't want \n in input what dxn should i go?? – Noob Mar 11 '22 at 17:02
  • "what dxn should i go" is unclear. If you do not want `'\n'` as input, do not enter it - perhaps close `stdin` after the `kkkkk`. Alternately, upon reading input with a `'\n'`, simply do not use that character in further code. Or since it is at the end of the string, when there, shorten the string. – chux - Reinstate Monica Mar 11 '22 at 17:31

0 Answers0