|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
String parsing program
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); while (isspace(s[i])) i++; while (isdigit(s[i])) i++; while (isspace(s[i])) i++; if (s[i] != '\0') printf("\nIncorrect string\n"); 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 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. |
|
#2
|
|||
|
|||
|
String parsing program
pereges wrote:
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); > while (isspace(s[i])) i++; while (isdigit(s[i])) i++; while (isspace(s[i])) i++; if (s[i] != '\0') printf("\nIncorrect string\n"); > return (0); } A string completely of whitespace will pass your test. <snip> |
|
#3
|
|||
|
|||
|
String parsing program
santosh wrote:
Your code exhibits undefined behaviour because you have failed to include ctype.h where the declarations for the is* functions are. Not really. The default declarations will do for those. It's not good practice, of course. Brian |
|
#4
|
|||
|
|||
|
String parsing program
pereges wrote:
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); Real bad example. > while (isspace(s[i])) while ((unsigned char) s[i]) i++; while (isdigit(s[i])) i++; This does not _require_ a digit to be present. while (isspace(s[i])) i++; if (s[i] != '\0') printf("\nIncorrect string\n"); 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 That is not a weakness but a strength. x = strtoul(str, &endp, 0); if (endp != str) while (isspace((unsigned char) endp)) endp++; if (endp != str && *endp == 0) /* all good */; for eg or -123 to some unsigned value is removed. if (endp != str && *endp == 0 && strchr(str,'-') == 0) /* all good */; -- Peter |
|
#5
|
|||
|
|||
|
String parsing program
pete wrote:
Default User wrote: But not undefined behavior. It's a better way to write C90 code, even if you don't intend it to be compiled as C99 code, What does this have to do with what I said? Brian |
|
#6
|
|||
|
|||
|
String parsing program
Default User wrote:
pete wrote: > >Default User wrote: > But not undefined behavior. > >It's a better way to write C90 code, >even if you don't intend it to be compiled as C99 code, > > What does this have to do with what I said? Nothing. There was nothing more to say on that topic. I hijack threads here very frequently to discuss what I want to discuss about the C programming language. -- pete |
![]() |
| Viewing: Web Development Archives > FAQs > C/C++ > String parsing program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|