fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <assert.h>
  5.  
  6. struct SameCharacter {
  7. SameCharacter(char character) : character(character) {}
  8. bool operator()(char c) { return c == character; }
  9.  
  10. const char character;
  11. };
  12.  
  13. void RemoveCharacterFromString(char character_for_remove, char *string) {
  14. char *end = string + strlen(string);
  15. char *it = std::remove_if(string, end, SameCharacter(character_for_remove));
  16. std::fill(it, end, '\0');
  17. }
  18.  
  19. void RemoveAllCharactersInRangeFromString(char from, char to, char *string) {
  20. assert(from <= to);
  21. for (char curr_character = from; curr_character <= to; ++curr_character) {
  22. RemoveCharacterFromString(curr_character, string);
  23. }
  24. }
  25.  
  26. char *Del_Useless(char *s) {
  27. RemoveAllCharactersInRangeFromString('j', 'z', s);
  28. RemoveAllCharactersInRangeFromString('J', 'Z', s);
  29. return s;
  30. }
  31.  
  32. int main() {
  33. char raw_str[100] = "aaa bbb ccc ddd ccc fff eee ggg fff jj asdf z asdf";
  34. std::cout << "Raw string : " << raw_str << '\n';
  35. char *cleaned_string = Del_Useless(raw_str);
  36. std::cout << "after remove: " << cleaned_string << '\n';
  37. std::cout << "Raw string : " << raw_str << '\n';
  38. }
  39.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
Raw string  : aaa bbb ccc ddd ccc fff eee ggg fff jj asdf  z asdf
after remove: aaa bbb ccc ddd ccc fff eee ggg fff  adf   adf
Raw string :  aaa bbb ccc ddd ccc fff eee ggg fff  adf   adf