-2

Aim is to make sure that the user entered input for string 1 and string 2 contains only characters A,T,G or C in any order. If either string contains another other character then error should be displayed. Example:

Input contains error

Error in String #1: aacgttcOgMa

Error in String #2: ggataccaSat

This is my attempt at LCS.cpp file code:

#include "LCS.h"
#include <string>

using namespace std;

bool validate(string strX, string strY)
{

string x = strX;
string y = strY;
char searchItem = 'A';
char searchItem = 'C';
char searchItem = 'G';
char searchItem = 'T';
int numOfChar = 0;
int m = strX.length();
int n = strY.length();
for (int i = 0; i < m; i++)
{
    if (x[i] == searchItem)
    {
        numOfChar++;

    }
for (int i = 0; i < n; i++)
    {
        if (y[i] == searchItem)
        {
            numOfChar++;

        }
}

}

This is my LCS.h file code:

#pragma once
#ifndef LCS_H
#define LCS_H

#include <string>

using namespace std;

bool validate(string strX, string strY);
#endif

And my driver file "Driver6.cpp" has this code:

#include "LCS.h"
#include <iostream>
#include <string>


using namespace std;

int main()
{
string strX, strY;

cout << "String #1: ";
cin >> strX;
cout << "String #2: ";
cin >> strY;

//validate the input two strings
if (validate(strX, strY) == false)
{
    return 0;
}

int m = strX.length();
int n = strY.length();

}
  • You appear to be trying to create 4 separate variables with the same name. All you need to do is loop over the string and check that each character is either A, G, T or C, using an if-statement. You don't need those variables. –  Nov 13 '18 at 21:13
  • @NeilButterworth could you show me how you write an if function which can look for two different strings? – ConfusedProgrammer Nov 13 '18 at 21:23
  • Look through the first string in a loop. Then look through the second string in another loop. And there is no reason for the validate function to take two strings as parameters anyway. –  Nov 13 '18 at 21:26
  • @NeilButterworth Does this look right for the first string? bool validate(string strX, string strY) { if (strX =! "A" || strX =! "T"|| strX =! "G", strX =! "C") { cout << "Input contains error" << endl; cout << "Error in String #1:" + strX << endl; cout << "Error in String #2:" + strY << endl; } } – ConfusedProgrammer Nov 13 '18 at 21:35
  • No, it doesn't. You need a loop and you do NOT need to validate both strings at once. –  Nov 13 '18 at 21:37
  • @NeilButterworth I think i now have a loop, but i know i am still doing something catastrophically wrong. Could you please tell me what it is? bool validate(string strX) { for(int i = 0; i < strX.size();i++) { if (strX = ! "A" || strX = ! "T" || strX =! "G" || strX =! "C"); cout << "Input contains error" << endl; cout << "Error in String #1:" + strX << endl; } } – ConfusedProgrammer Nov 13 '18 at 21:46
  • There is no such operator as `=!`, it is `!=` - and your ors should be ands. I've been resisting this, but it seems this could go on all night, so I'll post a solution as an answer. –  Nov 13 '18 at 21:48

2 Answers2

0

Didn't really want to do this but it seems like the best bet rather than going round the houses in the comments:

#include <string>
#include <iostream>

bool validate( const std::string & s ) {
    for ( auto c : s ) {
        if ( c != 'A' && c != 'T' && c != 'C' && c != 'G' ) {
            return false;
        }
    }
    return true;
}

int main() {
    std::string s1 = "ATGCCCG";
    std::string s2 = "ATGfooCCCG";

    if ( validate( s1 ) ) {
        std::cout << "s1 is valid\n";
    }
    else {
        std::cout << "s1 is not valid\n";
    } 
    if ( validate( s2 ) ) {
        std::cout << "s2 is valid\n";
    }
    else {
        std::cout << "s2 is not valid\n";
    } 
}
0

Another technique:

bool validate(const std::string& s)
{
  const static std::string valid_letters("ATCGatcg");
  for (auto c: s)
  {
     std::string::size_type position = valid_letters.find_first_of(c);
     if (position == std::string::npos)
     {
        return false;
     }
  }
  return true;
}

The above code searches a container of valid letters.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154