fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // A program to practice creating and calling a function to reverse a string
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. //declaring the function before use
  12. string reverseString(const string& input,string& output);
  13.  
  14.  
  15.  
  16. //main function
  17. int main()
  18. {
  19. string userInputStr;
  20.  
  21. cout << "Please enter a string to be reversed: ";
  22. cin >> userInputStr;
  23. string reversedString = "";
  24. reverseString(userInputStr,reversedString);
  25.  
  26. cout << userInputStr << " reversed is " << reversedString;
  27.  
  28. return 0;
  29. }
  30.  
  31. string reverseString(const string& input, string& output)
  32. {
  33. char characterInString;
  34. int counter;
  35. int lengthOfString = input.length();
  36.  
  37.  
  38. for (counter = 1; counter <= lengthOfString; counter++)
  39. {
  40. characterInString = input[(lengthOfString - counter)];
  41. output = output + characterInString;
  42. }
  43. return output;
  44.  
  45.  
  46. }
  47.  
Success #stdin #stdout 0s 3232KB
stdin
john
stdout
Please enter a string to be reversed: john reversed is nhoj