Guest User

Untitled

a guest
Aug 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. Check if one string is a prefix of another
  2. std::string foo("foo");
  3. std::string foobar("foobar");
  4.  
  5. auto res = std::mismatch(foo.begin(), foo.end(), foobar.begin());
  6.  
  7. if (res.first == foo.end())
  8. {
  9. // foo is a prefix of foobar.
  10. }
  11.  
  12. bool isPrefix(string const& s1, string const&s2)
  13. {
  14. const char*p = s1.c_str();
  15. const char*q = s2.c_str();
  16. while (*p&&*q)
  17. if (*p++!=*q++)
  18. return false;
  19. return true;
  20. }
  21.  
  22. bool
  23. unorderIsPrefix( std::string const& lhs, std::string const& rhs )
  24. {
  25. return std::equal(
  26. lhs.begin(),
  27. lhs.begin() + std::min( lhs.size(), rhs.size() ),
  28. rhs.begin() );
  29. }
  30.  
  31. // Yields true if the string 's' starts with the string 't'.
  32. bool startsWith( const std::string &s, const std::string &t )
  33. {
  34. return strncmp( s.c_str(), t.c_str(), t.size() ) == 0;
  35. }
  36.  
  37. // Yields true if the string 's' starts with the string 't'.
  38. template <class T>
  39. bool startsWith( const T &s, const T &t )
  40. {
  41. return std::equal( t.begin(), t.end(), s.begin() );
  42. }
  43.  
  44. #include <string>
  45. #include <iostream>
  46.  
  47. // does str1 have str2 as prefix?
  48. bool StartsWith(const std::string& str1, const std::string& str2)
  49. {
  50. return (str1.find(str2)) ? false : true;
  51. }
  52.  
  53. // is one of the strings prefix of the another?
  54. bool IsOnePrefixOfAnother(const std::string& str1, const std::string& str2)
  55. {
  56. return (str1.find(str2) && str2.find(str1)) ? false : true;
  57. }
  58.  
  59. int main()
  60. {
  61. std::string str1("String");
  62. std::string str2("String:");
  63. std::string str3("OtherString");
  64.  
  65. if(StartsWith(str2, str1))
  66. {
  67. std::cout << "str2 starts with str1" << std::endl;
  68. }
  69. else
  70. {
  71. std::cout << "str2 does not start with str1" << std::endl;
  72. }
  73.  
  74. if(StartsWith(str3, str1))
  75. {
  76. std::cout << "str3 starts with str1" << std::endl;
  77. }
  78. else
  79. {
  80. std::cout << "str3 does not start with str1" << std::endl;
  81. }
  82.  
  83. if(IsOnePrefixOfAnother(str2, str1))
  84. {
  85. std::cout << "one is prefix of another" << std::endl;
  86. }
  87. else
  88. {
  89. std::cout << "one is not prefix of another" << std::endl;
  90. }
  91.  
  92. if(IsOnePrefixOfAnother(str3, str1))
  93. {
  94. std::cout << "one is prefix of another" << std::endl;
  95. }
  96. else
  97. {
  98. std::cout << "one is not prefix of another" << std::endl;
  99. }
  100.  
  101. return 0;
  102. }
  103.  
  104. str2 starts with str1
  105. str3 does not start with str1
  106. one is prefix of another
  107. one is not prefix of another
  108.  
  109. string a = "String";
  110. string b = "String:";
  111.  
  112. if(b.find(a) == 0)
  113. {
  114. // Prefix
  115.  
  116. }
  117. else
  118. {
  119. // No Prefix
  120. }
  121.  
  122. bool prefix(const std::string& a, const std::string& b) {
  123. if (a.size() > b.size()) {
  124. return a.substr(0,b.size()) == b;
  125. }
  126. else {
  127. return b.substr(0,a.size()) == a;
  128. }
  129. }
  130.  
  131. #include <string>
  132. #include <iostream>
  133.  
  134. bool prefix(const std::string& a, const std::string& b);
  135.  
  136. int main() {
  137. const std::string t1 = "test";
  138. const std::string t2 = "testing";
  139. const std::string t3 = "hello";
  140. const std::string t4 = "hello world";
  141. std::cout << prefix(t1,t2) << "," << prefix(t2,t1) << std::endl;
  142. std::cout << prefix(t3,t4) << "," << prefix(t4,t3) << std::endl;
  143. std::cout << prefix(t1,t4) << "," << prefix(t4,t1) << std::endl;
  144. std::cout << prefix(t1,t3) << "," << prefix(t3,t1) << std::endl;
  145.  
  146. }
Add Comment
Please, Sign In to add comment