Why is the code is not removing “u” from the input string?

前端 未结 3 834
礼貌的吻别
礼貌的吻别 2021-01-19 16:22

I am learning C++. Today I have written a code to remove vowels form a string. It works fine in some tests. But this test is failing to remove "u" from a string. M

3条回答
  •  醉话见心
    2021-01-19 17:06

    Here's how you can do it more easily:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    constexpr auto isVowel(unsigned char const ch) noexcept {
      constexpr std::array vowels{
          'a', 'e', 'i', 'o', 'u',
      };
      return std::find(vowels.begin(), vowels.end(), std::tolower(ch)) !=
             vowels.end();
    }
    
    int main() {
      std::string str = "Tour";
      str.erase(std::remove_if(str.begin(), str.end(), isVowel), str.end());
      std::cout << str << '\n';
    }
    

    Try online

提交回复
热议问题