fork download
  1. // Timothy Price CS1A Ch 10 Challenge 2
  2. /******************************************************************************
  3.  * Reverse letters
  4.  *
  5.  * This program receives a word or set of characters and reverses the order.
  6.  *
  7.  * Input:
  8.  * a word or set of characters
  9.  *
  10.  * Output:
  11.  * the input in reverse order
  12. ******************************************************************************/
  13.  
  14. #include <iostream>
  15.  
  16. using namespace std;
  17.  
  18. // Function prototype
  19. void reverse(const char* str);
  20.  
  21. int main() {
  22.  
  23. // Data dictionary
  24. const int SIZE = 256;
  25. char input[SIZE];
  26. int count = 0;
  27.  
  28. // Receive input with confirmation
  29. cout << "Enter a word or phrase no longer than " << (SIZE - 1);
  30. cout << " characters: ";
  31. cin.getline(input, SIZE);
  32. cout << input << endl;
  33.  
  34. // Call function
  35. cout << "Now in reverse: ";
  36. reverse(input);
  37.  
  38. return 0;
  39. }
  40.  
  41. // Function definition
  42. void reverse(const char* str) {
  43.  
  44. // Exit statement
  45. if (str == nullptr || *str == '\0')
  46. {
  47. return;
  48. }
  49.  
  50. const char* end = str;
  51.  
  52. // Move pointer to the null terminator
  53. while (*end != '\0')
  54. {
  55. end++;
  56. }
  57.  
  58. // Step back to the last character, then print down to str
  59. end--;
  60. while (end >= str)
  61. {
  62. cout << *end;
  63. end--;
  64. }
  65. }
Success #stdin #stdout 0s 544KB
stdin
This statement is false!
stdout
Enter a word or phrase no longer than 255 characters: This statement is false!
Now in reverse: !eslaf si tnemetats sihT