0

When I use fgets() function to take input the string,the The result gave a garbage value at the end. But when I use gets() or scanf() function then the program work perfectly and accepted by the judge. I can't understand the issues while using fgets () to take input.

#include<stdio.h>
#include<string.h>
int main()
{
int k,i;
char s[100];
fgets (s,sizeof(s),stdin);
scanf("%d",&k);

for(i=0;s[i]!='\0';i++)
{
  if( (s[i]-k)<65) 
   {
     printf("%c",s[i]+26-k);
   }
  else printf("%c",s[i]-k);
}
return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    Please don't use ['magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). Please use [the standard character classification and manipulation functions](https://en.cppreference.com/w/c/string/byte). – Some programmer dude Oct 09 '22 at 15:39
  • 1
    @MD I also suggest you take some time to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your program. For example print the string directly after `fgets`. Print the value of `k` directly after `scanf`. And check what `fgets` *returns*. – Some programmer dude Oct 09 '22 at 15:41
  • @Someprogrammerdude My bad, I will change that. However, look at how he is reading from the stream. Doesn't he need to iterate until a termination character? – Sir Archibald Humphrey Oct 09 '22 at 15:42
  • @quandaledingle And that usage of [`fgets`](https://en.cppreference.com/w/c/io/fgets) is perfectly fine. Remember that [`fgets`](https://en.cppreference.com/w/c/io/fgets) reads a *string* (a whole line, if possible). Whatever problem the OP have, it's not the `fgets` call. I'll guess it's more to do with using a value of `k` that leads to unprintable or weird characters being printed. The OP probably doesn't understand ASCII encoding. – Some programmer dude Oct 09 '22 at 15:44
  • @Someprogrammerdude Oops ok thank you I need to brush up a little on those functions – Sir Archibald Humphrey Oct 09 '22 at 15:47
  • 4
    The string that `fgets` populates will include the trailing newline you typed in, assuming there's room for it. – dbush Oct 09 '22 at 15:52
  • 4
    [Never ever ever ever use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Every appearance of `gets` in code is a security vulnerability, arguably the worst possible kind of bug. For similar reasons, never use `scanf("%s", buf)` either. – Nate Eldredge Oct 09 '22 at 15:52
  • 1
    Continuing @dbush 's comment: the newline (almost certainly) has a value of 10. Unless `k` is negative, nl - k will be less than 65. (By which I suppose you mean `'A'`) but you should use `islower`, and you could simplify everything by using `tolower` and avoid the primary issue. – William Pursell Oct 09 '22 at 15:56
  • Note that if the input is "Foo\nx" you have undefined behavior. There are several other types of potential undefined behavior in this code, and it would be worthwhile to understand how they can arise. – William Pursell Oct 09 '22 at 15:58
  • fgets puts the end-of-line character '\n' in the buffer. you can remove it with this line `s[strcspn(s, "\n")] = '\0';` right after the fgets. – CGi03 Oct 09 '22 at 16:26
  • [There is no `gets` function in C](https://files.lhmouse.com/standards/ISO%20C%20N2176.pdf). – n. m. could be an AI Oct 09 '22 at 21:16

0 Answers0