fork download
  1. //Cameron Pham CS1A Chapter 10, p. 588, #2
  2. //
  3. /******************************************************************************
  4. * DISPLAY CONTENTS BACKWARD
  5. * ______________________________________________________________________________
  6. * This program accepts a pointer to a C-string and displays its characters in
  7. * reverse order.
  8. * ______________________________________________________________________________
  9. * INPUT
  10. * String // Characters inputted
  11. *
  12. * OUTPUT
  13. * Reverse // Characters put in reverse
  14. *******************************************************************************/
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. // Function prototype
  19. void displayBackward(char* str);
  20. int main()
  21. {
  22. const int SIZE = 100;
  23. char userString[SIZE];
  24. // Get a string from the user
  25. cout << "Enter a string: ";
  26. cin.getline(userString, SIZE);
  27. // Display the string backward
  28. cout << "String backward: ";
  29. displayBackward(userString);
  30. cout << endl;
  31. return 0;
  32. }
  33. // Display the characters of a C-string backward
  34. void displayBackward(char* str)
  35. {
  36. int length = 0;
  37. // Determine the length of the string
  38. while (*(str + length) != '\0')
  39. {
  40. length++;
  41. }
  42. // Display the characters in reverse order
  43. for (int count = length - 1; count >= 0; count--)
  44. {
  45. cout << *(str + count);
  46. }
  47. }
Success #stdin #stdout 0s 572KB
stdin
Gravity
stdout
Enter a string: String backward: ytivarG