Reputation: 13
I'm writing a program to find the location of a certain character in a string using strchr
. When using fgets
to input the string from the user, the program does not execute properly.
However when using gets
everything works fine.
Code that works using gets()
:
#include <stdio.h>
#include <string.h>
int main() {
char let;
char input[50];
char *ptr;
printf("what is the string that you would like to check?\n");
gets(input);
printf("what is the character that you would like to find?\n");
let = getchar();
printf("in string \"%s\"...\n", input);
ptr = strchr(input, let);
while (ptr != NULL) {
printf("the letter %c was found at character %d\n", let, ptr - input + 1);
ptr = strchr(ptr + 1, let);
}
return 0;
}
Output:
what is the string that you would like to check? what is the character that you would like to find? in string "why is the world when wondering"... the letter w was found at character 1 the letter w was found at character 12 the letter w was found at character 18 the letter w was found at character 23
Code that does not work usgin fgets()
:
#include <stdio.h>
#include <string.h>
int main() {
char let;
char input[50];
char *ptr;
printf("what is the string that you would like to check?\n");
fgets(input, 16, stdin);
printf("what is the character that you would like to find?\n");
let = getchar();
printf("in string \"%s\"...\n", input);
ptr = strchr(input, let);
while (ptr != NULL) {
printf("the character is found at %d \n", ptr - input + 1);
ptr = strchr(ptr + 1, let);
}
return 0;
}
Output:
what is the string that you would like to check? what is the character that you would like to find? in string "abcdefghijklmno"...
Upvotes: 1
Views: 135
Reputation: 3032
Change
fgets(input, 16, stdin)
to
fgets(input, sizeof(input), stdin)
When you pass an argument of 16
to fgets()
you are instructing it to read no more than 15 characters. Why?
From the fgets()
manpage:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.
If you provide more than size -1
characters, the remaining characters are left in the input buffer.
Then when the program subsequently calls
let = getchar();
let
is assigned whatever the next character in the input buffer is - without even waiting for you to type anything else. It then searches for this character in the string you provided - but in the example you provided doesn't find it.
Upvotes: 2
Reputation: 311126
In the first code snippet the call
gets (input);
does not restrict the number of characters the user can input. Take into account that the function gets
is not supported by the C Standard and is unsafe.
In the second code snippet you declared the variable input
as
char input[50];
So it is more natural to call fgets
the following way
fgets (input,sizeof( input ),stdin );
instead of
fgets (input,16,stdin);
The standard function getchar
reads any character including white spaces.
So instead it it is much better to use
scanf( " %c", &let );
Otherwise getchar
can read any character (including for example the new line character) leaved in the input buffer. While the call of scanf
will skip any white space character.
Upvotes: 0