fork download
  1. /*******************************************************************************
  2.  * Aiden Stannard // CS1A // Chapter 10 Homework: Challange 2# // 11/28/2026
  3.  * _____________________________________________________________________________
  4.  *
  5.  * String Reverser
  6.  * _____________________________________________________________________________
  7.  *
  8.  * This program accepts a CString and outputs the contents of the CString in
  9.  * reverse order.
  10.  * _____________________________________________________________________________
  11.  *
  12.  * Input: Enter a five letter word___
  13.  *
  14.  * Output: The enterd five letter word is___ in reverse
  15.  *
  16.  ******************************************************************************/
  17. #include <iostream>
  18. #include <cctype>
  19. #include <cstring>
  20.  
  21. using namespace std;
  22.  
  23. void reverse(char *, int);
  24.  
  25. int main() {
  26. int size = 6;
  27. char test[size] = "Hello"; // c string
  28. char *ptr = test; // c string pointer
  29.  
  30. cout << "Enter a five letter word " << endl;
  31. cin >> test; // user enters word
  32. reverse(ptr, size); // The string pointer and c string size is passed to the
  33. // function
  34. return 0;
  35. }
  36.  
  37. void reverse(char *test, int size){
  38.  
  39. int x = size - 2;
  40. cout << x << endl;
  41.  
  42. for (int i = 0; i < (size - 1) and !(x == i); i++)
  43. {
  44. char temp = test[i]; // reversed string by swapping the first element
  45. test[i] = test[x]; // with the last element, incrementing/dec those
  46. test[x] = temp; // elements until they equal one another, ending the loop
  47. x--;
  48. }
  49. cout << "The entered five letter word, is " << test << " in reverse" << endl;
  50. }
Success #stdin #stdout 0s 544KB
stdin
balls
stdout
Enter a five letter word 
4
The entered five letter word, is sllab in reverse