See this main
:
int main(void)
{
int i;
int ch;
char str[512];
fgets(str, sizeof str, stdin);
for (i = 0; i <= (strlen(str)); i++)
{
if (str[i] != '\0' && str[i] != '\n')
{
int num = atoi(&str[i]);
printf("%d\n", num);
}
}
return 0;
}
I want to get line with numbers from user and get all the numbers without any spaces
or tabs
.
For example:
The input 1 2 3
.
But in this case this the output:
1
2
2
3
3
So why i received 2
and 3
twice?
Simple integer input in C, using `fgets()` to get the entire line as a string and `strtol()` to parse the integer.
Return Value The fgets() function returns a pointer to the string buffer if successful. A NULL return value indicates an error or an end-of-file condition. Use the feof() or ferror() functions to determine whether the NULL value indicates an error or the end of the file.
Even though both the functions, gets() and fgets() can be used for reading string inputs. The biggest difference between the two is the fact that the latter allows the user to specify the buffer size. Hence it is highly recommended over the gets() function.
fgets terminates at the newline character but appends it at the end of the string str . The function also appends the terminating null character at the end of the passed string.
fgets() Function in C. The syntax of the fgets() function is: Syntax: char *fgets(char *str, int n, FILE *fp); The function reads a string from the file pointed to by fp into the memory pointed to by str.
fgets () function reads string from a file, one line at a time. fputs () function writes string to a file. feof () function finds end of file. fgetchar () function reads a character from keyboard. fprintf () function writes formatted data to a file. fscanf () function reads formatted data from a file.
int fgetc (FILE *pointer) pointer: pointer to a FILE object that identifies the stream on which the operation is to be performed. The entire content of file is printed character by character till end of file. It reads newline character as well. fputc () is used to write a single character at a time to a given file.
Following is the declaration for fgets () function. str − This is the pointer to an array of chars where the string read is stored. n − This is the maximum number of characters to be read (including the final null-character).
Here's how I would do it:
char line[256];
if (fgets(line, sizeof line, stdin) != NULL)
{
const char *ptr = line;
while (*ptr != '\0')
{
char *eptr = NULL;
const long value = strtol(ptr, &eptr, 10);
if (eptr != ptr)
printf("%ld\n", value);
else
break;
ptr = eptr;
}
}
This uses strtol()
so it will also handle negative numbers; if this is incorrect you can of course add checks to filter them out. I think this is way better than anything using strtok()
.
Because the you also pass the position of the string which starts with spaces. they get the first number to be 2
and 3
respectively twice. That is what is returned.
for (i = 0; i <= (strlen(str)); i++)
{
if (str[i] != '\0' && !isspace(str[i]) )
{
int num = atoi(&str[i]);
printf("%d\n", num);
}
}
Prints:
1
2
3
For the purpose of tokenizing you can use strtok
and to convert it to number strtol
etc. These provides much better control over the error cases than atol/atoi
do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With