C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 3rd, 2008, 02:49 PM
pereges
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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.

Reply With Quote
  #2  
Old July 3rd, 2008, 05:30 PM
santosh
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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>


Reply With Quote
  #3  
Old July 3rd, 2008, 06:49 PM
Default User
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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





Reply With Quote
  #4  
Old July 3rd, 2008, 11:09 PM
Peter Nilsson
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #5  
Old July 4th, 2008, 06:39 AM
Default User
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #6  
Old July 4th, 2008, 09:19 AM
pete
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > String parsing program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway