dragfire
dragfire

Reputation: 443

String comparison in cpp

I am trying to compare strings in cpp but i got a bunch of errors like
no match for 'operator=='
invalid conversion from char to const char *

I'm implementing two functions :
1) compare the strings and search for brackets, then return a bool.
2) compare the strings and search for arithmetic operators, then return a bool.

Here's my code :

bool isBracket(const string b)
{
    if(b == ")" || b=="(")
            return true;
    else
            return false;
}

bool isOperator(const string op)
{
    string ops= "*+-^/";
    for(int i = 0; i < ops.length(); i++)
    {
            if (op == ops[i])
                    return true;
            else 
                    return false;
    }
}

int main()
{
    string exp="(a+b)";

    for(int i=0; i < exp.size(); i++)
    {
        cout<<exp[i]<<endl;
        if(isBracket(exp[i]))
                cout<<"bracket found"<<endl;
        if(isOperator(exp[i]))
                cout<<"operator found"<<endl;
    }
    return 0;
}

Upvotes: 0

Views: 1404

Answers (3)

fkarg
fkarg

Reputation: 198

Your isOperator() function would always return false, because (as mentioned) you try to compare a string with a char. on the other hand, it wouldn't even work if you would compare a char with a char, but you'd have to try out every single char in the op string.

AND you'd have to change the return statement after the if(). instead, return false only if it went through the whole for loop.

someone else did post some even more useful code, but another question from me, wouldn't it be more useful to return where the operator (and maybe even which operator) has been found?

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

The functions could look the following way

bool isBracket( const string &b )
{
    return b.find_first_of( "()" ) != std::string::npos;
}

bool isOperator( const string &op )
{
    return op.find_first_of( "*+-^/" ) != std::string::npos;
}

Or if you are comparing only elements of a string then the functions could look as

#include <cstring>
//...

bool isBracket( char b )
{
            const char *brackets = "()"; 
            return std::strchr( brackets, b ) != NULL; 

}

bool isOperator( char op )
{
            const char *ops = "*+-^/";
            return std::strchr( ops, op ) != NULL; 
}

Or even like

#include <cstring>
//...

bool isBracket( char b )
{
            const char *brackets = "()"; 
            return b != '\0' && std::strchr( brackets, b ) != NULL; 

}

bool isOperator( char op )
{
            const char *ops = "*+-^/";
            return op != '\0' && std::strchr( ops, op ) != NULL; 
}

Upvotes: 4

Erik Alap&#228;&#228;
Erik Alap&#228;&#228;

Reputation: 2713

The comparison op == ops[i]

compares a std::string and a char, if op is a one-char string you could do op[0] == ops[i] instead.

Also, I suggest you look at a good reference book, e.g. Josuttis "The C++ Standard Library: A Tutorial and Reference". There are also decent online references, just Google for them. There you can find a lot of useful string functions, and STL algorithms may also be useful, as other posters have already pointed out.

Upvotes: 2

Related Questions