fork(7) download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstring>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11. //Variables and Arrays
  12.  
  13. char Phrase[80];
  14.  
  15. char Reverse[80];
  16.  
  17. char* Palindrome = Reverse;
  18.  
  19. int i, j, test = 0;
  20.  
  21. cout << "Please enter a sentence to be reversed: ";
  22. cin >> Phrase;
  23.  
  24. cin.getline(Phrase, 80);
  25. int length = strlen(Phrase);
  26.  
  27. for(i = 0; i < (length/2); i++) // do a loop from 0 to half the length of the string
  28. {
  29. if(test = 1) // if it is a palindrome so far
  30. {
  31. if(Phrase[i] != Phrase[length-i-1]) // check the characters match
  32. {
  33. test = 0; // if they don't set the indicator to false
  34. }
  35. }
  36. else
  37. {
  38. break; // if it is not a palindrome, exit the for loop
  39. }
  40. }
  41.  
  42. if(test == 1)
  43. {
  44. cout << "Phrase/Word is a Palindrome." << endl;
  45.  
  46. for(j = strlen(Phrase) - 1; j >= 0; Palindrome++, j--)
  47. {
  48. *Palindrome = Phrase[j];
  49. cout << "The reverse is: " << Reverse << endl << endl;
  50. }
  51. }
  52. else
  53. {
  54. cout << "Phrase/Word is not a Palindrome." << endl;
  55. }
  56.  
  57. system("Pause");
  58. return 0;
  59. }
Success #stdin #stdout #stderr 0s 3344KB
stdin
aab
stdout
Please enter a sentence to be reversed: Phrase/Word is not a Palindrome.
stderr
sh: Pause: not found