stringReverser.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Programmer: Jonathan Landrum
  3. * Date: 25 February 2012
  4. *
  5. * Program: stringReverser.cpp
  6. * Purpose: Takes string input and reverses it
  7. * Assumptions: None.
  8. */
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12. // --------------------------------------------------
  13. // main():
  14. // --------------------------------------------------
  15. int main () {
  16. // Variables
  17. int counter;
  18. int counter2;
  19. string input;
  20. // Introduce the program
  21. cout << endl;
  22. cout << "-----------------------------" << endl;
  23. cout << "- C++ String Reverser -" << endl;
  24. cout << "-----------------------------" << endl;
  25. cout << endl;
  26. cout << "This program takes a string" << endl;
  27. cout << "from the user and reverses it." << endl;
  28. cout << endl;
  29. // Main processing loop
  30. cout << "Enter a string to reverse:" << endl;
  31. getline(cin, input);
  32. char array[input.length()];
  33. // Push the string into an array backwards
  34. counter2 = 0;
  35. for (counter = input.length() - 1; counter >= 0; --counter) {
  36. array[counter2] = input[counter];
  37. ++counter2;
  38. } // End for
  39. for (counter = 0; counter < input.length(); ++counter) {
  40. cout << array[counter];
  41. } // End for
  42. cout << endl;
  43. // Return the results
  44. return (0);
  45. } // End main