how to manipulate string

I have this string that consist of numbers

it is one string that has multiple numbers with new lines

when i print out the string, it looks like

50.23
40.21
39.30
and so on

I was wondering how I could compare the values to find out the highest number

I was trying to use sscanf to convert the string to float values but the number of values can vary thus I found it not suitable

can anyone suggest a efficient way of finding the highest number out of the numbers that are stored as string format.
You can build your own very easily. C++ string class provide at(), size(), operator[] etc for you to do what you want.

You can start compare from left to right. If the string1 leftmost character is higher string2 leftmost character, then can terminate checking and decide the string1 is bigger. Of cuz there are other minor details like what if . is not inside should still consider, leading, trailing spaces counted or not etc etc.

Idea is there are enough methods in C++ string class for you to build your own comparison.
I would probably store the first string a variable and compare each new string to it in a loop and if it is greater than it will overwrite it and so on since you only want the highest number and not the highest and lowest.

But I am a newb so this is probably.

so basically I would try:

grab string (however you are getting it)



highest number = string;

while(you can grab string) {

if (string > highest number) {
highest number = string;
}
}


This probably too basic for what you want but this is how I would attempt it.


PS- Oh, so all numbers are stored as string format. Can you typecast them all to floats and then compare?
Last edited on
Topic archived. No new replies allowed.