I have a string which looks like this “a 3e,6s,1d,3g,22r,7c 3g,5r,9c 19.3”, how do I go through it and extract the integers and assign them to its corresponding letter variable?. (i have integer variables d,r,e,g,s and c). The first letter in the string represents a function, “3e,6s,1d,3g,22r,7c” and “3g,5r,9c” are two separate containers . And the last decimal value represents a number which needs to be broken down into those variable numbers.
my problem is extracting those integers with the letters after it and assigning them into there corresponding letter. and any number with a negative sign or a space in between the number and the letter is invalid. How on earth do i do this?
The description of the string format is not really clear but I think I can answer your question anyway (extracting the integers with the letters and adding(?) them to the proper int variable).
So beginning with this string:
char* was = “3e,6s,1d,3g,22r,7c”; // was == weird ass string
it is probably easiest to tokenize it using strtok.
Now you can use sscanf to find the number and letter.
With regards to the other quirks with your string format (the separate containers and the float at the end (others??)), you can handle those in similar ways. Just think of it like pealing an onion, work your way through the format layer by layer until you get to the letter number combination.
Additionally any format faults will be caught at the very least when sscanf is invoked.