Search In
• More options...
Find results that contain...
Find results in...

# Help C++

Go to solution Solved by vm'N,

what pattern exactly ?

Psuedo-pattern:

*h*e*l*l*o*

I can't get any further in the problem

here's my code

`#include<iostream>#include<string>#include<algorithm>using namespace std;bool check(string,string,string);int main(){    bool right[4];    string s1,m,n;    cin>>s1;    int z=s1.find("h");    s1.erase(0,z);    for(int i=0;i<s1.size();i++)    {        if(s1[i]!='h'&&s1[i]!='e'&&s1[i]!='l'&&s1[i]!='o')             {                  s1.erase(i,1);             }    }  for(int i=0;i<4;i++)    {        if(i==0)        {            m="h";            n="e";        }        else if(i==1)        {            m="e";            n="l";        }        else if(i==2)        {            m="l";            n="l";        }        else if(i==3)        {            m="l";            n="o";        }        if(check(s1,m,n)==true)     //n,m are the range             right[i]=true;            else                right[i]=false;    }    if (right[0]==true&&right[1]==true&&right[2]==true&&right[3]==true)        cout<<"YES"<<endl;    else        cout<<"NO"<<endl;    return 0;}bool check(string s1,string s2,string s3){    int x=s1.find(s2);    int z=s1.find(s3);    if(s2=="l")        z=s1.rfind(s3);    for(int i=x;i<z;i++)    {            if (s1.find(s2) != string::npos)                return true;    }    return false;}`

Wrong test case

`8Time: 0 ms, memory: 0 KBVerdict: WRONG_ANSWERInputhatlevhhalrohairnolsvocafgueelrqmlqlleelloOutputNOAnswerYESChecker commentwrong answer 1st words differ - expected: 'YES', found: 'NO'`
##### Share on other sites

Have a look at Regex (Regular Expressions). Its part of c++ and a really clean way of doing this:

http://www.cplusplus.com/reference/regex/regex_search/

Desktop: Intel i9-10850K (R9 3900X died  )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

##### Share on other sites

You should be able to solve this with one pass through the input.

`// pseudo-codeCharArray wordToFind; //ex: helloCharArray input; // ehosnsejsllofor each char in wordToFind:    find input[x]; // x would start at zero    if found:        continue looking for input[x+1] on the next character    Repeat until word is found or you run out of characters to check`
##### Share on other sites

Please, drop the horrible single character variable naming, make a habit of naming them by their purpose. Just a word of advice

##### Share on other sites

Could you please add comments to your code, explaining what you are trying to do? Right now it's a bit difficult to find out what you are actually trying to do.

- Max Oomen

##### Share on other sites

I would recommend regex instead.

You would be able to do something like:

`regex_match("Insert word here", regex("Insert pattern here"))`
##### Share on other sites

I would recommend regex instead.

You would be able to do something like:

`regex_match("Insert word here", regex("Insert pattern here"))`

what pattern exactly ?

##### Share on other sites

what pattern exactly ?

Psuedo-pattern:

*h*e*l*l*o*

##### Share on other sites

Psuedo-pattern:

*h*e*l*l*o*

I get the regex thing and start to understand it but this pattern "*h*e*l*l*o*" is giving me an error

##### Share on other sites

I get the regex thing and start to understand it but this pattern "*h*e*l*l*o*" is giving me an error

A '*' character by itself doesn't make sense, it means "zero or more of the previous character" so that '*' at the beginning is breaking it. You need a '.' before the '*'s which is regex for any character, so '.*' means any number of any characters.

1474412270.2748842

##### Share on other sites

I get the regex thing and start to understand it but this pattern "*h*e*l*l*o*" is giving me an error

I know it wouldn't work. But it should lead you to the right direction.

The asterisk (*) means that there can be zero or more characters.

A '*' character by itself doesn't make sense, it means "zero or more of the previous character" so that '*' at the beginning is breaking it. You need a '.' before the '*'s which is regex for any character, so '.*' means any number of any characters.

##### Share on other sites

I know it wouldn't work. But it should lead you to the right direction.

The asterisk (*) means that there can be zero or more characters.

A '*' character by itself doesn't make sense, it means "zero or more of the previous character" so that '*' at the beginning is breaking it. You need a '.' before the '*'s which is regex for any character, so '.*' means any number of any characters.

Accepted thanks guys

`#include<iostream>#include<string>#include<algorithm>#include<regex>using namespace std;int main(){    string s1;    cin >> s1;    regex e(".*h.*e.*l.*l.*o.*");    bool match = regex_match(s1, e);    cout << (match ? "YES" : "NO") << endl << endl;    return 0;}`
##### Share on other sites

alphabeta Personally I would still try approaching this problem without using regular expressions.  The reason I say this by using things like regular expressions to solve these problems is you don't learn necessarily how to program.  You can know a language, but to me programming is about how you approach problems and how you build your solutions.

While knowing regex is good, and in general this would be an ideal real world solution, you aren't really learning too much by using regex in this case.

Here is my ansi c version of a solution (Sorry I am into java right now, so I am not comfortable with the C++ libraries at the moment), but the approach to the problem is the important thing imho.

`/* containsHelloinputText: the input characters, assumes input is not a null pointer and    assumes input is 1+ characters long, null terminated and only lowercaseoutput: 0 failed to find hello    1 hello was foundGeneral approach.The thing to recognize is you only need to find the letters h, e, l, l, o in that orderYou don't have to worry about erasing things from the inputText, but just need to rememberwhich character you have found in the target word and start searching for the next characterafter it like what madknight3 was sayingActually this is probably a lot quicker than regular expressions in the sense of run time,given that regular expressions should have a fair amount of overhead. Although this isn'treally optimized so I guess if someone wants to test by they can (assuming I got this correct)p.s. I haven't even compiled this code, so it could fail miserably*/int containsHello(const char* inputText) {    int indexForTargetWord = 0;    int indexForInputText = 0;    const char* targetWord = "hello";    /* So indexForInputText will be 0, and we will compare the inputText's character    against the targetWord's character.    If it is a match, we will start searching for the next character in targetWord    If it is not a match, we need to keep incrementing indexForInputText until we find one    If we reach the end of inputText then we know the targetword's characters never ended up    in the inputText's text    */    for(indexForTargetWord = 0; targetWord[indexForTargetWord] != 0; indexForTargetWord++) {        /* We are comparing here, but */        while(inputText[indexForInputText] != targetWord[indexForTargetWord]) {            if(inputText[indexForInputText] == 0)                return 0; /* We have reached the end, and have not found the word */            indexForInputText++;        }        /* Getting here means we found the character, but we still need to look one character        in the future for next time, so increment */        indexForInputText++;    }    /* we have matched the full word if we hit this return */    return 1;}`

0b10111010 10101101 11110000 00001101

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account