View Full Version : Extract integers from a string
Toniglandyl
9 Nov 2004, 10:14am
Hello,
I'm trying to write a program that would prints out any numbers contained in a string along with the number of numbers found.
I have no clue on how to do it, could someone help me with this?
Thanks in advance :)
mmonnin
9 Nov 2004, 11:47am
What kind of string? What language?
Best idea I can think of is to make a loop and compare each object in the string and check to see if the object was 0-9. If it is 0-9 it would up a counter which would take care of your number of numbers. Within the loop you could print the character.
Toniglandyl
9 Nov 2004, 12:18pm
Oh sorry the language used is MIPS. I would know how to do it using Java but in MIPS I have absolutely no idea :confused:
I would like to process any string like "I have 22 trousers" and the procedure shoulkd return "22" and "2" because there are 2 numbers
Here is some explanations about what I have to do:
[QUOTE]
One parameter should be passed to this routine which contains the address of the
start of a null-terminated ASCII string, which already exists in memory. The
routine will search for the first character in the string that is a decimal digit 0-9.
It will then take this, and any consecutive characters that are digits, and work out
the actual decimal value they represent, as an unsigned integer.
The routine will return two values, the unsigned integer that represents the ASCII
digits it has processed and the address of the character following the last digit
converted. If no digits can be found then the routine should return zero in both
cases.
This is easiest to understand with an example. If the string was “I like being 22
more than I liked being 11, I think” then one value will be 22, and the other
would hold the address of the space character just before the word “more”.
QUOTE]
mmonnin
9 Nov 2004, 4:58pm
Well I cant help you there. I could only do it in C++.
TheBaron
9 Nov 2004, 5:42pm
you ought to be able to parse strings in mips in a similar fashion to the way you do in C (hell I can do this in three different kinds of assembly [but i dont know mips], it cant be that hard :D)
i mean its easy enough, when you find a character representing a number check to see if the next character also represents a number and if it does multiply the first digit by 10 and add the current one, then add to some array you're building
and your strings are either null terminated or you're given a length (otherwise you wouldn't know how big the string is) so you have a definitive end
vBulletin® v3.8.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.