How to make sure that two strings only have certain alphabets in c++










-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();











share|improve this question






















  • 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.

    – Neil Butterworth
    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.

    – Neil Butterworth
    Nov 13 '18 at 21:26











  • @NeilButterworth Does this look right for the first string? bool validate(string strX, string strY) strX =! "T"

    – 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.

    – Neil Butterworth
    Nov 13 '18 at 21:37















-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();











share|improve this question






















  • 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.

    – Neil Butterworth
    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.

    – Neil Butterworth
    Nov 13 '18 at 21:26











  • @NeilButterworth Does this look right for the first string? bool validate(string strX, string strY) strX =! "T"

    – 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.

    – Neil Butterworth
    Nov 13 '18 at 21:37













-2












-2








-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();











share|improve this question














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();








c++ string dynamic char lcs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 21:11









ConfusedProgrammerConfusedProgrammer

11




11












  • 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.

    – Neil Butterworth
    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.

    – Neil Butterworth
    Nov 13 '18 at 21:26











  • @NeilButterworth Does this look right for the first string? bool validate(string strX, string strY) strX =! "T"

    – 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.

    – Neil Butterworth
    Nov 13 '18 at 21:37

















  • 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.

    – Neil Butterworth
    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.

    – Neil Butterworth
    Nov 13 '18 at 21:26











  • @NeilButterworth Does this look right for the first string? bool validate(string strX, string strY) strX =! "T"

    – 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.

    – Neil Butterworth
    Nov 13 '18 at 21:37
















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.

– Neil Butterworth
Nov 13 '18 at 21:13






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.

– Neil Butterworth
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





@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.

– Neil Butterworth
Nov 13 '18 at 21:26





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.

– Neil Butterworth
Nov 13 '18 at 21:26













@NeilButterworth Does this look right for the first string? bool validate(string strX, string strY) strX =! "T"

– ConfusedProgrammer
Nov 13 '18 at 21:35






@NeilButterworth Does this look right for the first string? bool validate(string strX, string strY) strX =! "T"

– 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.

– Neil Butterworth
Nov 13 '18 at 21:37





No, it doesn't. You need a loop and you do NOT need to validate both strings at once.

– Neil Butterworth
Nov 13 '18 at 21:37












2 Answers
2






active

oldest

votes


















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 validn";

else
std::cout << "s1 is not validn";

if ( validate( s2 ) )
std::cout << "s2 is validn";

else
std::cout << "s2 is not validn";







share|improve this answer























  • Thank you so much. After about an hour, I finally now understand it!!

    – ConfusedProgrammer
    Nov 13 '18 at 22:47


















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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289537%2fhow-to-make-sure-that-two-strings-only-have-certain-alphabets-in-c%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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 validn";

    else
    std::cout << "s1 is not validn";

    if ( validate( s2 ) )
    std::cout << "s2 is validn";

    else
    std::cout << "s2 is not validn";







    share|improve this answer























    • Thank you so much. After about an hour, I finally now understand it!!

      – ConfusedProgrammer
      Nov 13 '18 at 22:47















    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 validn";

    else
    std::cout << "s1 is not validn";

    if ( validate( s2 ) )
    std::cout << "s2 is validn";

    else
    std::cout << "s2 is not validn";







    share|improve this answer























    • Thank you so much. After about an hour, I finally now understand it!!

      – ConfusedProgrammer
      Nov 13 '18 at 22:47













    0












    0








    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 validn";

    else
    std::cout << "s1 is not validn";

    if ( validate( s2 ) )
    std::cout << "s2 is validn";

    else
    std::cout << "s2 is not validn";







    share|improve this answer













    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 validn";

    else
    std::cout << "s1 is not validn";

    if ( validate( s2 ) )
    std::cout << "s2 is validn";

    else
    std::cout << "s2 is not validn";








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 21:51









    Neil ButterworthNeil Butterworth

    27.1k54680




    27.1k54680












    • Thank you so much. After about an hour, I finally now understand it!!

      – ConfusedProgrammer
      Nov 13 '18 at 22:47

















    • Thank you so much. After about an hour, I finally now understand it!!

      – ConfusedProgrammer
      Nov 13 '18 at 22:47
















    Thank you so much. After about an hour, I finally now understand it!!

    – ConfusedProgrammer
    Nov 13 '18 at 22:47





    Thank you so much. After about an hour, I finally now understand it!!

    – ConfusedProgrammer
    Nov 13 '18 at 22:47













    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.






    share|improve this answer



























      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.






      share|improve this answer

























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 22:15









        Thomas MatthewsThomas Matthews

        44.3k1173122




        44.3k1173122



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289537%2fhow-to-make-sure-that-two-strings-only-have-certain-alphabets-in-c%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto