fork download
  1. //Devin Scheu CS1A Chapter 10, P. 588, #2
  2. //
  3. /**************************************************************
  4. *
  5. * DISPLAY C-STRING BACKWARD
  6. * ____________________________________________________________
  7. * This program reverses and displays the contents of a C-string.
  8. * ____________________________________________________________
  9. * INPUT
  10. * inputString : The C-string entered by the user
  11. *
  12. * OUTPUT
  13. * reversedString : The reversed contents of the C-string
  14. *
  15. **************************************************************/
  16.  
  17. #include <iostream>
  18. #include <iomanip>
  19.  
  20. using namespace std;
  21.  
  22. // Function to display C-string backward using pointer notation
  23. void displayBackward(const char* str) {
  24. const char* end = str;
  25. while (*end != '\0') {
  26. end++;
  27. }
  28. end--; // Move back to the last character
  29. while (end >= str) {
  30. cout << *end;
  31. end--;
  32. }
  33. cout << endl;
  34. }
  35.  
  36. int main () {
  37.  
  38. //Variable Declarations
  39. const int MAX_SIZE = 100; //OUTPUT - Maximum size of the input string
  40. char inputString[MAX_SIZE]; //INPUT - The C-string entered by the user
  41. string reversedString; //OUTPUT - The reversed contents of the C-string
  42.  
  43. //Prompt for Input and Echo
  44. cout << "Enter a string (max 99 characters): ";
  45. cin.getline(inputString, MAX_SIZE);
  46. cout << inputString << endl;
  47.  
  48. //Separator and Output Section
  49. cout << "-------------------------------------------------------" << endl;
  50. cout << "OUTPUT:" << endl;
  51.  
  52. //Display Backward
  53. cout << left << setw(25) << "Reversed String:" << right;
  54. displayBackward(inputString);
  55.  
  56. } //end of main()
Success #stdin #stdout 0.01s 5284KB
stdin
Racecar
stdout
Enter a string (max 99 characters): Racecar
-------------------------------------------------------
OUTPUT:
Reversed String:         racecaR