How to know if a word is before another

closed account (GywfSL3A)
Hi,

I would like to know how I can know if a word is before another in a string. Here's my current code.
(I have some code after that, that's there's so many #include)
Thank you for your help

#include <iostream>
#include <math.h>
#include <string>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <iterator>

using namespace std;

int main() {

string equation;
getline (cin, equation);

unsigned answer = equation.find("=");
string equal = equation.substr (answer);

unsigned pos = equation.find("x");
string x = equation.substr (pos);

// unsigned reponse = equation.find("=");
// string egal = equation.substr (reponse);

if ('x' > 'equal') { cout << "x is before equal";}
else {cout << "equal is before x";}
closed account (GywfSL3A)
Please
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <sstream>

// return true if in str, substring a appears before substring b
bool before( const std::string& str, const std::string& a, const std::string& b )
{
    const auto apos = str.find(a) ;
    const auto bpos = str.find(b) ;

    static constexpr auto npos = std::string::npos ;
    return apos != npos && bpos != npos && apos < bpos ;
}

// return true if in str, word a appears before word b
// where a word is defined as a white-space delimited substring
bool word_before( const std::string& str, const std::string& a, const std::string& b )
{
    std::istringstream stm(str) ;
    std::string word ;
    while( stm >> word && word != a ) ;
    while( stm >> word && word != b ) ;
    return stm ;
}

int main()
{
    const std::string& str = "how now brown cow" ;
    std::cout << std::boolalpha
               << before( str, "now", "cow" ) << '\n' // true
               << before( str, "now", "wow" ) << '\n' // false
               << before( str, "bow", "cow" ) << '\n' // false
               << before( str, "how now", " brow" ) << '\n' ; // true

    std::cout << "-------\n"
               << word_before( str, "now", "cow" ) << '\n' // true
               << word_before( str, "now", "wow" ) << '\n' // false
               << word_before( str, "ho", "cow" ) << '\n' // false
               << word_before( str, "how", "brow" ) << '\n' ; // false
}

http://coliru.stacked-crooked.com/a/c0276ad872a721b4
closed account (GywfSL3A)
Thank you very much for your time and answer, the only problem is that this is not working, there's the error : http://textup.fr/75927XI

Pretty big error.

Every help is accepted
C++ 98

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <sstream>

// return true if in str, substring a appears before substring b
bool before( const std::string& str, const std::string& a, const std::string& b )
{
    const std::size_t apos = str.find(a) ;
    const std::size_t bpos = str.find(b) ;

    static const std::size_t npos = std::string::npos ;
    return apos != npos && bpos != npos && apos < bpos ;
}

// return true if in str, word a appears before word b
// where a word is defined as a white-space delimited substring
bool word_before( const std::string& str, const std::string& a, const std::string& b )
{
    std::istringstream stm(str) ;
    std::string word ;
    while( stm >> word && word != a ) ;
    while( stm >> word && word != b ) ;
    return stm.good() ;
}

int main()
{
    const std::string& str = "how now brown cow" ;
    std::cout << std::boolalpha
               << before( str, "now", "cow" ) << '\n' // true
               << before( str, "now", "wow" ) << '\n' // false
               << before( str, "bow", "cow" ) << '\n' // false
               << before( str, "how now", " brow" ) << '\n' ; // true

    std::cout << "-------\n"
               << word_before( str, "now", "cow" ) << '\n' // true
               << word_before( str, "now", "wow" ) << '\n' // false
               << word_before( str, "ho", "cow" ) << '\n' // false
               << word_before( str, "how", "brow" ) << '\n' ; // false
}

http://coliru.stacked-crooked.com/a/00190fc06bf40e3f
closed account (GywfSL3A)
Hi,

Thank you very much for what you give me, it work perfectly. However, I would like to change the "b" (const std::string& b) with "=", I can change the "a" with "x", but "=" is not a primary expression so it don't work.

I would like it to be : if x is before =, say true

Same things than before, but with "x" and "=".
Thank you again.

Cordially
Last edited on
> However, I would like to change the "b" (const std::string& b) with "=",
> I can change the "a" with "x", but "=" is not a primary expression so it don't work.
> I would like it to be : if x is before =, say true
> Same things than before, but with "x" and "=".

I do not understand what this means - it is not clear to me at all.

Please post a clear example illustrating what it is that you are trying to do.
closed account (GywfSL3A)
Sure, sorry for that.

Here's what I want do do :
Normally, my code do ---> if b is before x, say true and if x is before b, say false.
What I want is ---> if = is before x, say true and if x is before =, say false

So, I think I have to do that to make it work :


My code is that :

----------

#include <iostream>
#include <math.h>

using namespace std;

// return true if in str, substring a appears before substring b
bool before( const string& str, const string& x, const string& b )
{
const size_t apos = str.find('x') ;
const size_t bpos = str.find('b') ;

static const size_t npos = string::npos ;
return apos != npos && bpos != npos && apos < bpos ;
}

int main() {

string str;
getline (cin, str);
cout << boolalpha
<< before( str, "x", "b" ) << '\n' // true
<< before( str, "b", "x" ) << '\n' ; // false

}

----------

I want to change it to this :

-------------


#include <iostream>
#include <math.h>

using namespace std;

// return true if in str, substring a appears before substring b
bool before( const string& str, const string& x, const string& = )
{
const size_t apos = str.find('x') ;
const size_t bpos = str.find('=') ;

static const size_t npos = string::npos ;
return apos != npos && bpos != npos && apos < bpos ;
}

int main() {

string str;
getline (cin, str);
cout << boolalpha
<< before( str, "x", "=" ) << '\n' // true
<< before( str, "=", "x" ) << '\n' ; // false

}

--------------

But when I want to change the "b" to "=" in :

---
bool before( const string& str, const string& x, const string& b )
---
It say to me :
---
prog.cpp:7:66: error: expected primary-expression before ‘)’ token
bool before( const string& str, const string& x, const string& = )
---

What can I do to resolve that?
Thank you

I hope this is more clear
Last edited on
First read up on functions:
http://msdn.microsoft.com/en-us/library/f81cdka5(v=vs.110).aspx
http://crasseux.com/books/ctutorial/Actual-parameters-and-formal-parameters.html


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

// return true if in str, substring a appears before substring b
bool before( const std::string& str, const std::string& a, const std::string& b )
{
    const std::size_t apos = str.find(a) ;
    const std::size_t bpos = str.find(b) ;

    static const std::size_t npos = std::string::npos ;
    return apos != npos && bpos != npos && apos < bpos ;
}

int main()
{
    const std::string str = "x + y*y = z" ;

    // does "x" appear before "=" ?
    bool x_is_before_eq = before( str, "x", "=" ) ;
    std::cout << std::boolalpha << x_is_before_eq << '\n' ; // true

    const std::string x = "x" ;
    const std::string eq = "=" ;
    std::cout << std::boolalpha << before( str, x, eq ) << '\n' ; // true

    // does "y*y" appear before "=" ?
    std::cout << std::boolalpha << before( str, "y*y", "=" ) << '\n' ; // true

    // does "x" appear before "y" ?
    std::cout << std::boolalpha << before( str, "x", "y" ) << '\n' ; // true

    // does "+" appear before "y*y" ?
    std::cout << std::boolalpha << before( str, "+", "y*y" ) << '\n' ; // true

    // etc.
}
closed account (GywfSL3A)
Thank you for all your help, you gived me a good start.
Topic archived. No new replies allowed.