0

I'm attempting to write alil function here which basically reads from a string. It reads every three characters and evaluates it using the pre-condition (if statement). If conditions are met, it would replace those three letters with new three letters. Then it would output the new string.

I tried writing the code but cant seem to get the logic right. the program runs but it doesn't print out anything. Dont mind the function name and the inaccuracy. I'm just doing a sample function to test this out.

string amino_acids(string line) { string acid; string acids; string newline; for( int i= 0; i < line.length(); i++) { acid = line[i]; } for (int i = 0; i < 3; i++) { acids = acid[i]; if(acids == "GUU") { acids = "ZAP"; } newline = acids; } cout << "Acids: " <<newline <<endl; return newline; } 
5
  • Your braces are in the wrong places. I think you meant the second for loop to be inside the first one, among other things. Commented Mar 21, 2013 at 2:27
  • for( int i= 0; i < line.length(); i++) { acid = line[i]; } is equivalent to if (!line.empty()) acid = line.back();. Is that what you intended? Commented Mar 21, 2013 at 2:28
  • The if inside the for loop doesn't seem to make any sense. Why would you want to repeat that if test 3 times? Commented Mar 21, 2013 at 2:33
  • yeah im just confused at my own code lol Commented Mar 21, 2013 at 2:34
  • @user2188311, Being confused at your own code is an awfully fun feeling, isn't it? That's what we generally strive to avoid, but coming back to old code especially can do that. Commented Mar 21, 2013 at 2:54

4 Answers 4

1
for( int i= 0; i < line.length(); i++) acid = line[i]; 

Say line contains "abcd", this loop is going to do:

acid = 'a'; acid = 'b'; acid = 'c'; acid = 'd'; 

Only the last assignment has any lasting affect. If you need to actually get three characters from line into acid - you probably want to use += to add characters into acid, rather than =. But, if you loop over all of line like this, you'll end up doing acid = line;. I assume you want something more like acid = line.substr(0, 3)?

for (int i = 0; i < 3; i++) { acids = acid[i]; 

This is going to crash. acid is definitely a single character string, and you're indexing into acid[1] and acid[2] on the 2nd and 3rd iterations. While you're learning C++, you should probably use .at(i) which will throw an exception when you attempt to use an invalid index - you can catch the exception and at least have some indication of the problem. As is, it's undefined behaviour.

To use at, you need a try / catch block... the basic form is:

int main() try { ...your code in here... some_string.at(i); } catch (const std::exception& e) { std::cerr << "caught exception: " << e.what() << '\n'; } 

More generally, try putting some std::cout statements throughout your code so you know what values your variables actually have... you would easily have seen that they weren't what you expected. Alternatively, use an interactive debugger and watch the affect of each statement's execution.

Sign up to request clarification or add additional context in comments.

Comments

1

Indexing a std::string with the [] operator yields a char, for which there just happens to be an overloaded operator= for strings.

Even if you were looping as I believe you intended (which, as the comments on the question mention, you probably aren't) because acids (which takes the value of a single character) will never be equal to the three character string you're comparing it to. Thus, no replacements will be performed.

To do what you want, try something like this:

for (int i = 0; i + 3 < line.length(); i += 3) // counting by 3 until end of line { if (line.substr(i, 3) == "GUU") // if the substring matches { line.assign("ZAP", i, 3); // overwrite it with new substring } } return line; 

1 Comment

okay so maybe indexing the string is a bad idea. but isnt there a function that reads a certain amount of characters in a string?
0

Reading from your description, you want something like so

//note below does not compile, its just psuedo-code string amino_acid(const string& sequence){ string result = sequence; //make copy of original sequence For i = 0 to sequence.length - 3 string next3Seq = sequence(i,3); //grab next 3 character from current index If next3Seq == 'GUU' //if the next next three sequence is 'GUU' then result.replace(i,3,'ZAP'); //replace 'GUU' with 'ZAP' EndIf EndFor return result; } 

You can use that as a start to code. Good Luck.

Comments

0

According to my understanding of your question. I have written some code. Please look below

string acids; string newLine; int limit=1; for(int i=0;i<line.length();i++) { acids=acids+line[i]; if(limit==3)//Every 3 characters { if(acids == "GUU") { acids = "ZAP"; } limit=1; acids="" newline=newline+acids; } limit++; return newline; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.