user2188311
user2188311

Reputation: 1

Read from string (possible array usage?) C++

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;
}

Upvotes: 0

Views: 89

Answers (4)

rishikesh tadaka
rishikesh tadaka

Reputation: 483

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;
}

Upvotes: 0

dchhetri
dchhetri

Reputation: 7136

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.

Upvotes: 0

Wug
Wug

Reputation: 13196

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;

Upvotes: 1

Tony Delroy
Tony Delroy

Reputation: 106244

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.

Upvotes: 1

Related Questions