B
Barry Schwarz
Hi I've a string input and I have to parse it in such a way that that
there can be only white space till a digit is reached and once a digit
is reached, there can be only digits or white space till the string
ends. Am I doing this correctly ? :
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[50];
int i = 0;
gets(s);
You have to be trolling to still use this.
while (isspace(s))
i++;
while (isdigit(s))
i++;
while (isspace(s))
i++;
if (s != '\0')
printf("\nIncorrect string\n");
If the input is "9 5", you will fail the string even though it meets
your verbal definition.
return (0);
}
I want to actually convert a string to unsigned long. So this kind of
algorithm should be carried out prior to strtoul function to ensure
that some of the weakness from which the strtoul function suffers like
convertin 123aaaaa to 123 for eg or -123 to some unsigned value is
You can call this a weakness if you like but strtoul will provide you
enough info to detect the situation with a lot less code than if you
do it yourself.
removed. This will also ensure that when you have a string like :
1234 78
1234 is not returned but an error message will be printed. Because a
string should only contain 1 number in my program.
Except that a couple of messages down in this thread you state
explicitly that you want to accept this type of input.
Remove del for email