fork download
  1. // Diego Martinez CSC5 Chapter 10, P.588, #2
  2. /*******************************************************************************
  3. * DISPLAY REVERSE STRING
  4. * ______________________________________________________________________________
  5. * This program asks the user to enter a string. A function is used to display
  6. * the contents of the C-string backward.
  7. *
  8. * Computation is based on the Formula:
  9. *
  10. * Display characters from the end of the string to the beginning
  11. *
  12. * ______________________________________________________________________________
  13. * INPUT
  14. *
  15. * User string
  16. *
  17. * OUTPUT
  18. *
  19. * String displayed backward
  20. *
  21. *******************************************************************************/
  22.  
  23. #include <iostream>
  24. #include <cstring>
  25. using namespace std;
  26.  
  27. // Function prototype
  28. void backwardString(const char *);
  29.  
  30. int main()
  31. {
  32. // Variable declaration
  33. char userString[100];
  34.  
  35. // User input
  36. cout << "Enter a string: ";
  37. cin.getline(userString, 100);
  38.  
  39. // Function call
  40. cout << "The string backward is: ";
  41. backwardString(userString);
  42.  
  43. cout << endl;
  44.  
  45. return 0;
  46. }
  47.  
  48. // Function definition
  49. void backwardString(const char *str)
  50. {
  51. int length = strlen(str);
  52.  
  53. // Display string backward
  54. for (int i = length - 1; i >= 0; i--)
  55. {
  56. cout << *(str + i);
  57. }
  58. }
Success #stdin #stdout 0s 5324KB
stdin
DIEGO
stdout
Enter a string: The string backward is: OGEID