fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const size_t STRING_LENGTH = 12;
  5.  
  6. void removeLetter(char* string, char letterToBeRemoved)
  7. {
  8. // pBack will look at each letter in the array
  9. // and copy only valid letters to pFront
  10. // pFront is incremented only when valid letters are copied to it.
  11. char* pBack = string;
  12. char* pFront = string;
  13. while ((pBack - string) < STRING_LENGTH)
  14. {
  15. if (*pBack != letterToBeRemoved)
  16. {
  17. *pFront = *pBack;
  18. pFront++;
  19. }
  20. pBack++;
  21. }
  22.  
  23. // Terminate string if we removed something
  24. if ((pBack - string) != (STRING_LENGTH - 1))
  25. {
  26. *pFront = '\0';
  27. }
  28.  
  29.  
  30. }
  31.  
  32. int main() {
  33. char input[STRING_LENGTH] = "hello world";
  34. removeLetter(input, 'l');
  35. std::cout << input << std::endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
heo word