c++ - How to search a string for a character, and remove said character -
i'm trying code word game, , in word game, need able know letters have available me. have string available letters, @ start, "abcdefghijklmnopqrstuvwxyzaeiou", entire alphabet set of vowels. need able search string character (i might want use letter 'c'), , assuming character in string, remove character string. i'm not entirely sure how this, i'm writing in pseudocode.
string alphabet = "abcdefghijklmnopqrstuvwxyzaeiou" char input; cout << "please input character."; cin >> input; if (input in string) { remove letter string } else { cout << "that letter not available you." }
i think can use string::find find character, don't know how i'd able remove letter string. if there's better way go this, please let me know.
how search string character, , remove said character
just use std::remove
, , erase-remove idiom:
#include <string> #include <algorithm> #include <iterator> .... std::string s = .....; // string char c = ....; // char removed s.erase(std::remove(std::begin(s), std::end(s), c), std::end(s));
this c++03 version, in case you're stuck pre-c++11 compiler:
#include <string> #include <algorithm> .... std::string s = .....; // string char c = ....; // char removed s.erase(std::remove(s.begin(), s.end(), c), s.end());
Comments
Post a Comment