How to check if one string is the first half of another string? (Visit for explanation)

Hello.

I was trying to make a function that
checks if a string contains another string
and if the other string is the first part of
that string. For example, the function would
return true if "ten" was the first part of
"tent" Of course, it is. But these two strings
would vary, of course.

What I want is for the function to return true
if, and only if, the first string included
the second string, AND, that the second string
was the FIRST part of the first string. In other
words, it would return FALSE if it was "ten" and
"mitten", but TRUE if it was "ten" and "tent".

So, does anyone have any ideas?

Thanks.
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
43
#include <iostream>
#include <string_view>
#include <string>
#include <cctype>

// return true if the second non-empty string is the FIRST part of the first string.
// std::string_view: see https://www.cplusplus.com/forum/beginner/240488/
bool begins_with( const std::string_view& str, const std::string_view& substr )
{
    // true if substr is not empty, substr is found in str
    // and the position of the first character of the found substring is zero
    // https://en.cppreference.com/w/cpp/string/basic_string_view/find
    return !substr.empty() && str.find(substr) == 0 ;
}

// return a string with all characters converted to lower case
std::string to_lower( const std::string_view& str )
{
    std::string result ;

    // https://en.cppreference.com/w/cpp/string/byte/tolower
    // unsigned char: "To use these functions safely with plain chars (or signed chars),
    // the argument should first be converted to unsigned char"
    // https://en.cppreference.com/w/cpp/string/byte/tolower#Notes
    for( unsigned char c : str ) result += std::tolower(c) ;

    return result ;
}

// same as begins_with, but ignore case
bool begins_with_nocase( const std::string_view& str, const std::string_view& substr )
{
    return to_lower(str).find( to_lower(substr) ) == 0 ;
}

int main()
{
    std::cout << std::boolalpha
              << begins_with( "tent", "ten" ) << '\n' // true
              << begins_with( "TENT", "ten" ) << '\n' // false
              << begins_with_nocase( "TENT", "ten" ) << '\n' // true
              << begins_with( "mitten", "ten" ) << '\n' ; // false
}
Does you compiler implement C++20 std::string.starts_with()?
https://en.cppreference.com/w/cpp/string/basic_string/starts_with
string::compare() will do it with one line: http://www.cplusplus.com/reference/string/string/compare/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;

bool isPrefix( const string &str, const string &prefix)
{
    return (str.compare(0, prefix.size(), prefix) == 0);
}

int main()
{
    string str, substr;
    while (getline(cin, str) && getline(cin, substr)) {
	if (isPrefix(str, substr)) {
	    cout << substr << " is a prefix of " << str << '\n';
	} else {
	    cout << substr << " is not a prefix of " << str << '\n';
	}
    }
}


Input:
tent
ten
ten
tent
Hello World
World
Hello World
Hello


Output:
ten is a prefix of tent
tent is not a prefix of ten
World is not a prefix of Hello World
Hello is a prefix of Hello World


Thank you all. :)
Topic archived. No new replies allowed.