code.cpp 788 B

12345678910111213141516171819202122
  1. /* Create a function that takes a string, removes all "special" characters
  2. * (e.g. ! @ # $ % ^ & \ *) and returns the new string. The only
  3. * non-alphanumeric characters allowed are dashes -, underscores _ and spaces.
  4. */
  5. std::string removeSpecialCharacters(std::string str) {
  6. std::string::iterator it = str.begin();
  7. for(;it!=str.end();it++){
  8. // check character in string to see if it is alphanumeric
  9. bool alpha = isalnum(*it);
  10. bool notdash = *it != '-';
  11. bool notunderscore = *it != '_';
  12. bool notspace = *it != ' ';
  13. // if a non-alphanumeric character is found, remove it from the string.
  14. // do not remove the character if it is a space, underscore or dash.
  15. if(alpha==false && notdash && notunderscore && notspace){
  16. str.erase(it);
  17. it--;
  18. }
  19. }
  20. return str;
  21. }