And using this function, I made some useful functions.
Thank you.
...
void printAndCountAllCh(string str) { int count = 0; for (std::string::iterator it = str.begin(); it != str.end(); ++it) { std::cout << count << ": " << *it << endl; count++; } } string changeAtoB(string str, char A, char B) { int count = 0; for (std::string::iterator it = str.begin(); it != str.end(); ++it) { if (string(1, *it) == string(1, A)) { *it = B; } } return str; } string deleteA(string str, char A) { string rstr; int count = 0; for (std::string::iterator it = str.begin(); it != str.end(); ++it) { if (string(1, *it) != string(1, A)) { rstr.push_back(*it); } } return rstr; } int countNotCH(string str, char CH) { int count = 0; for (std::string::iterator it = str.begin(); it != str.end(); ++it) { if (string(1, *it) != string(1, CH)) count++; } return count; } int main() { string str = "A_B CD_EF GH_I"; int count; printAndCountAllCh(str); //print each char and count std::cout << endl; string abc = changeAtoB(str, ' ', '_'); //A_B CD_EF GH_I -> A_B_CD_EF_GH_I std::cout << abc << endl << endl; string bbb = deleteA(str, ' '); //A_B CD_EF GH_I -> A_BCD_EFGH_I std::cout << bbb << endl << endl; count = countNotCH(str, '_'); //A_B CD_EF GH_I -> 11 std::cout << count << endl << endl; return 0; }...
No comments:
Post a Comment