fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 10 P. 588 #2
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Backwards String
  6.  * ____________________________________________________________________________
  7.  * This program will accept a string from the user and then compute the
  8.  * backwards string for the user.
  9.  * ____________________________________________________________________________
  10.  * Input
  11.  * userInput :User's string they decide to input
  12.  * Output
  13.  * displayBackwards :The user's string now backwards
  14.  *****************************************************************************/
  15. #include <iostream>
  16. #include <string>
  17. using namespace std;
  18.  
  19. //Function Prototype
  20. void displayBackwards(const char *str);
  21.  
  22. int main() {
  23. string userInput;
  24.  
  25. cout << "Enter a string: " << endl;
  26. getline(cin, userInput);
  27.  
  28. displayBackwards(userInput.c_str());
  29.  
  30. return 0;
  31. }
  32. //Function Definition
  33. void displayBackwards(const char *str){
  34. int length = 0;
  35. while(str[length] != '\0'){
  36. length++;
  37. }
  38. //Display
  39. cout << "Your string backwards is: " << endl;
  40. for(int i = length - 1; i >= 0; i--){
  41. cout << str[i];
  42. }
  43. cout << endl;
  44. }
Success #stdin #stdout 0.01s 5324KB
stdin
chicken
stdout
Enter a string: 
Your string backwards is: 
nekcihc