bool function problem here

It suppose to find "END" or "end" at the end of source string and if there is the word "end" at the end of the string , it should return true , else false

Thank you !

Here is the code :


bool endcheck(string source,int len)
{
if (source.substr(source.length()-1 , 3 ) == "end")
{
return true ;
}
else if (source.substr(source.length()-1,3) == "END")
{
return true;
}
else
{
return false ;
}


}
Last edited on
Be careful to read the documentation for string methods. The first argument must be index of the first (leftmost) character in the substring. The second argument may be omitted to extract all remaining characters. So:

if (source.substr(source.length()-4) == "end")

Hope this helps.
Hello muratk

If you are going to use "substr" you are not backing up far enough to check the last three characters of the string.

Take a look at http://www.cplusplus.com/reference/string/string/substr/

And try "- 3" instead of "- 1" and see what happens.

You could also make use of the find function of the string class:

http://www.cplusplus.com/reference/string/string/find/

The else statement is not really necessary. If neither if statement is true then just let the function return false.

Hope that helps,

Andy

Edit: After reading this refer to Duthomhas's message in regards to the "- 3" I misunderstood.
Last edited on
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
41
42
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

// return true if string str ends with the string suffix
bool ends_with( const std::string& str, const std::string& suffix )
{
    if( str.size() < suffix.size() ) return false ;

    // http://en.cppreference.com/w/cpp/algorithm/equal
    else return std::equal( suffix.rbegin(), suffix.rend(), str.rbegin() ) ;
}

// return true if string str ends with the string suffix (ignore case)
bool ends_with_icase( const std::string& str, const std::string& suffix )
{
    if( str.size() < suffix.size() ) return false ;

    // http://www.stroustrup.com/C++11FAQ.html#lambda
    static const auto eq_ncase = [] ( unsigned char a, unsigned char b )
    { return std::tolower(a) == std::tolower(b) ; };

    // http://en.cppreference.com/w/cpp/algorithm/equal
    return std::equal( suffix.rbegin(), suffix.rend(), str.rbegin(), eq_ncase ) ;
}

bool endcheck( const std::string& source )
{ return ends_with( source, "END" ) || ends_with( source, "end" ) ; }

bool endcheck_icase( const std::string& source )
{ return ends_with_icase( source, "END" ) ; }

int main()
{
    std::cout << std::boolalpha
              << endcheck( "vvv nnn bbb end" ) << '\n' // true
              << endcheck( "vvv nnn bbb enD" ) << '\n' // false
              << endcheck_icase( "vvv nnn bbb EnD" ) << '\n' // true
              << endcheck( "vvv nnn bbb end!" ) << '\n' // false
              << endcheck_icase( "vvv nnn bbb end!" ) << '\n' ; // false
}

http://rextester.com/KYEI56973
Registered users can post here. Sign in or register to post.