I need to take a small string (for example: "FK3I) and convert it to "FKIII". As you can see, when a number is found (the number will always be greater than 3, thus there is no "FK2I", only "FKII") I need to take the letter after it and insert that many letters back to the string. I'm not quite sure how to "walk through" a string though. I know I need to set it up something like:
WHILE LOOP
// loop across the characters of the protein
check if the current char is not a digit
add it to the end of the decode string
(then look at next char)
check if the current char is a digit (convert it to int)
loop for the number of occurrences
add the next char after current char to the end of the decode string
But what kind of condition is supposed to go in the while loop and how am I supposed to look at each char in the string and then add it to the end, etc. Thanks so much for the help.
As Albatross noted you can check if the current character is a digit with the isdigit library function.
To iterate through the string in an effective way is somewhat more tricky...
A good idea is an infinite loop (while(true) or for(;;)), inside which you use the find_if function.
(take a look at this: http://cplusplus.com/reference/algorithm/find_if/ )
A string::iterator would be useful here. Declare one and initialize it to the beginning of your string (str.begin()). Then use find_if with your iterator, str.end() and your IsDigit function. This is important, you can't use the library's isdigit directly coz its prototype doesn't match what find_if expects. Just make a wrapper of the library's function that returns a bool. Here is an example:
1 2 3 4
bool IsDigit(char c)
{
return isdigit(c);
}
If find_if returns str.end() we are done... It means that no digits were found. In this case use break to stop the loop. Else it will return an iterator pointing at the place of the digit. Use this info and the string reference Albatross provided to see how you can do the replacement you want.
Sorry guys I'm pretty new to this and I'm also pretty bad and I'm not quite sure how to implement the things you all are talking about. I wrote this code but the only problem is I need to somehow make sure the statement under the first "if" resets so that it will be "decode[loop + (whatever the number previously used in the last "if" statement was)]". Because with this code all I get is "TTSMGYYYRHRBEHYYDNJDIGGYYYYY" for the example "5TSMGYYYRHRBEH8YDNJDI3G5Y". As you can see, the only thing that's right is the last "5Y" part. I know what my problem is but I can't seem to figure out how to make the first if statement change based on whether the second one has been done yet. I know this probably isn't the easiest or best way to do it, but this is all I know and I'd prefer to leave it this way.
You can do this. Think of the simplest solution possible. Take your string, and move character by character using the at() function. If isdigit(yourstring.at(i)) returns true and the digit you get from atoi() is greater than 2, replace that with the next letter, and add (the number you found, use atoi() - 2) more of that letter using insert(). There's no need for all that you have now.
This is all I could come up with from that. I'm still stumped :/ I just can't seem to put it all together. All these suggestions LOOK useful but when it comes time to try to implement them, I just get lost.
I have corrected that and written something else that I have gone through with pencil and paper and I see NO reason why this should not work, but lo and behold I get a message saying "debug assertion failed, string subscript out of range". This is getting frustrating, I've been working on this for hours.
Just give us an example of the type of string you are passing to the function to decode, and what you expect the result to be
and let's see what we can find.
I believe it might have something to do with the condition for my while loop because just out of curiosity I changed the condition to "loop < 5" and the error went away. Just sayin...