Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Check if one string is a prefix of another
- std::string foo("foo");
- std::string foobar("foobar");
- auto res = std::mismatch(foo.begin(), foo.end(), foobar.begin());
- if (res.first == foo.end())
- {
- // foo is a prefix of foobar.
- }
- bool isPrefix(string const& s1, string const&s2)
- {
- const char*p = s1.c_str();
- const char*q = s2.c_str();
- while (*p&&*q)
- if (*p++!=*q++)
- return false;
- return true;
- }
- bool
- unorderIsPrefix( std::string const& lhs, std::string const& rhs )
- {
- return std::equal(
- lhs.begin(),
- lhs.begin() + std::min( lhs.size(), rhs.size() ),
- rhs.begin() );
- }
- // Yields true if the string 's' starts with the string 't'.
- bool startsWith( const std::string &s, const std::string &t )
- {
- return strncmp( s.c_str(), t.c_str(), t.size() ) == 0;
- }
- // Yields true if the string 's' starts with the string 't'.
- template <class T>
- bool startsWith( const T &s, const T &t )
- {
- return std::equal( t.begin(), t.end(), s.begin() );
- }
- #include <string>
- #include <iostream>
- // does str1 have str2 as prefix?
- bool StartsWith(const std::string& str1, const std::string& str2)
- {
- return (str1.find(str2)) ? false : true;
- }
- // is one of the strings prefix of the another?
- bool IsOnePrefixOfAnother(const std::string& str1, const std::string& str2)
- {
- return (str1.find(str2) && str2.find(str1)) ? false : true;
- }
- int main()
- {
- std::string str1("String");
- std::string str2("String:");
- std::string str3("OtherString");
- if(StartsWith(str2, str1))
- {
- std::cout << "str2 starts with str1" << std::endl;
- }
- else
- {
- std::cout << "str2 does not start with str1" << std::endl;
- }
- if(StartsWith(str3, str1))
- {
- std::cout << "str3 starts with str1" << std::endl;
- }
- else
- {
- std::cout << "str3 does not start with str1" << std::endl;
- }
- if(IsOnePrefixOfAnother(str2, str1))
- {
- std::cout << "one is prefix of another" << std::endl;
- }
- else
- {
- std::cout << "one is not prefix of another" << std::endl;
- }
- if(IsOnePrefixOfAnother(str3, str1))
- {
- std::cout << "one is prefix of another" << std::endl;
- }
- else
- {
- std::cout << "one is not prefix of another" << std::endl;
- }
- return 0;
- }
- str2 starts with str1
- str3 does not start with str1
- one is prefix of another
- one is not prefix of another
- string a = "String";
- string b = "String:";
- if(b.find(a) == 0)
- {
- // Prefix
- }
- else
- {
- // No Prefix
- }
- bool prefix(const std::string& a, const std::string& b) {
- if (a.size() > b.size()) {
- return a.substr(0,b.size()) == b;
- }
- else {
- return b.substr(0,a.size()) == a;
- }
- }
- #include <string>
- #include <iostream>
- bool prefix(const std::string& a, const std::string& b);
- int main() {
- const std::string t1 = "test";
- const std::string t2 = "testing";
- const std::string t3 = "hello";
- const std::string t4 = "hello world";
- std::cout << prefix(t1,t2) << "," << prefix(t2,t1) << std::endl;
- std::cout << prefix(t3,t4) << "," << prefix(t4,t3) << std::endl;
- std::cout << prefix(t1,t4) << "," << prefix(t4,t1) << std::endl;
- std::cout << prefix(t1,t3) << "," << prefix(t3,t1) << std::endl;
- }
Add Comment
Please, Sign In to add comment