fork download
  1. //Cecilia Rangel CSC5 Chapter 10, P.588, #2
  2. //
  3. /**************************************************************
  4.  *
  5.  * Backwards string
  6.  * ____________________________________________________________
  7.  * This program returns an integer and accepts a
  8.  * pointer to a C-string as an arguement. The
  9.  * function flips the characters in the
  10.  * string and returns that string.
  11.  *_____________________________________________________________
  12.  * INPUT
  13.  * userinput : INPUT a string from the user
  14.  *
  15.  * OUTPUT
  16.  * backwards : OUTPUT the string from the user backwards
  17.  *
  18.  * ***************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22. using namespace std;
  23.  
  24. void DisplayBackwards(char* ptr);
  25.  
  26. int main()
  27. {
  28. //
  29. //Declare varaibles and pointers
  30. int SIZE = 100; //Initialize size to 100
  31. char userInput[SIZE]; //User input
  32. char* ptr; //Pointer to character type
  33. ptr = userInput; //Assign pointer to array
  34. //
  35. //Prompt user to enter character string
  36. cout << "Enter a character string.\n";
  37. cin.getline(userInput, SIZE);
  38. //
  39. //Call function
  40. DisplayBackwards(ptr);
  41. return 0;
  42. }
  43. //
  44. //Display string backwards
  45. void DisplayBackwards(char* ptr)
  46. {
  47. int count;
  48. int size = 0;
  49. int element = 0;
  50. //
  51. //Find size of string array
  52. for (count = 0; *(ptr + count) != '\0'; count++)
  53. size++;
  54. //
  55. //Display string backwards
  56. for (element = size; element >= 0; element--)
  57. {
  58. cout << *(ptr + element);
  59. }
  60. }
Success #stdin #stdout 0s 544KB
stdin
hard candy christmas
stdout
Enter a character string.
samtsirhc ydnac drah