473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Parsing a String to grab numbers

46
Hi,
Once again, Google has failed me.
I need to know how to parse a string that contains two integer numbers separated by a comma (ex: 6,3). Even though the two numbers are supposed to be single digits, I have to be prepared to handle a number of any size. So pretty much, I have to input a string formatted in x,y notation, and then grab both numbers (x and y) and put them in two integer variables (int x, and int y). The only way I've been able to think of is to loop through the string, test each character against a switch statement, and if it's a number, add it to a C string. When a comma is detected, convert the C string to an integer and dump it to int x, then do the same thing until the end of the string is found. This way seems so laborious and messy, however. Is there any "cleaner" way to do it?
Thanks,
Austen
Apr 27 '07 #1
4 6003
svlsr2000
181 Expert 100+
Hi,
Once again, Google has failed me.
I need to know how to parse a string that contains two integer numbers separated by a comma (ex: 6,3). Even though the two numbers are supposed to be single digits, I have to be prepared to handle a number of any size. So pretty much, I have to input a string formatted in x,y notation, and then grab both numbers (x and y) and put them in two integer variables (int x, and int y). The only way I've been able to think of is to loop through the string, test each character against a switch statement, and if it's a number, add it to a C string. When a comma is detected, convert the C string to an integer and dump it to int x, then do the same thing until the end of the string is found. This way seems so laborious and messy, however. Is there any "cleaner" way to do it?
Thanks,
Austen
you can use atoi, after call to atoi move your pointer to begining of next integer.
Apr 27 '07 #2
hi,

hey if u wanna parse a string and take the values from it... use the STRTOK frunction...
for instance

char str[]="65,32";

if u wanna parse this string then,
char *ptr;
int x,y;
ptr=strtok(str,",");
x=atoi(ptr);
ptr=strtok(NULL,",");
y=atoi(ptr);


this is one way u can parse a string rather than goin char by char which is more tedious....

hope it solves ur prob....

Kars
Apr 27 '07 #3
Banfa
9,065 Expert Mod 8TB
use strtol, this converts a character array (string) to an integer and returns a pointer to where the converstion failed/stopped.

So you should be able to

Expand|Select|Wrap|Line Numbers
  1. char input = "243,574";
  2. char *pEnd;
  3. long x, y;
  4.  
  5. x = strtol ( input, &pEnd, 10 );
  6. if (*pEnd == ',')
  7. {
  8.     pEnd++;
  9.     y = strtol ( pEnd, &pEnd, 10 );
  10.  
  11.     if (*pEnd != '\0')
  12.     {
  13.         // Conversion Error
  14.     }
  15. }
  16. else
  17. {
  18.     // Conversion Error
  19. }
  20.  
Apr 27 '07 #4
sake
46
Thank you very much :-)
-Austen
Apr 27 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Tuang | last post by:
I've been looking all over in the docs, but I can't figure out how you're *supposed* to parse formatted strings into numbers (and other data types, for that matter) in Python. In C#, you can say...
8
by: jamesfin | last post by:
Dear XMLers, Please guide me... I have a simple xml file... <URLTest>
4
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally...
0
by: Sundown | last post by:
I am making a program that needs to grab a string input ie: "green * 10 + white *10 + Blue *10 + Yellow *10 + Red * 20" I am looking to parse only the numbers and colors out of the string. I...
1
by: yoda715 | last post by:
Hey guys, I am currently stumped on a problem. I am looking for a way to easily grab text out of a string. Example, I have a string that looks like this: aaa bbbb cccc dddd What I need to do...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
3
by: mtuller | last post by:
Alright. I have tried everything I can find, but am not getting anywhere. I have a web page that has data like this: <tr > <td headers="col1_1" style="width:21%" > <span class="hpPageText"...
0
by: Ahmed, Shakir | last post by:
Thanks everyone who tried to help me to parse incoming email from an exchange server: Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve...
1
by: hd95 | last post by:
In a perfect world my xml feed source would produce perfect xml ..that is not the case I am parsing an XML feed that sometimes has ampersands and dashes in the content that messes up my parsing. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.