fork download
  1. // Andres Guzman CSC5 Chapter 10, P. 588, #2
  2. /******************************************************************************
  3.  *
  4.  * REVERSE DICTIONARY WORD
  5.  * ____________________________________________________________________________
  6.  * Program function will accept a c-string as an argument and output the
  7.  * entered string in reverse.
  8.  *
  9.  * Computation:
  10.  *
  11.  * Function will compute using a "strlen" command and a for loop
  12.  * ____________________________________________________________________________
  13.  *
  14.  * INPUT
  15.  *
  16.  * SIZE = Array size of characters
  17.  * word = entered c-string
  18.  *
  19.  * OUTPUT
  20.  *
  21.  * reverse = Entered input reversed
  22.  *
  23.  ******************************************************************************/
  24. #include <iostream>
  25. #include <cstring>
  26. using namespace std;
  27.  
  28. void reverse(char *a); //Function Prototype
  29.  
  30. int main()
  31. {
  32. const int SIZE = 1000; //Array Size Based on the Longest English Word
  33. char word[SIZE]; //Array of Characters
  34. //
  35. //Input Prompt
  36. cout << "Enter a Word: ";
  37. cin.getline(word, SIZE);
  38. reverse(word); //Function Call and output
  39. return 0;
  40. }
  41. /*******************************************************************************
  42.  * Adding Definition to function. This function will calculate the total length
  43.  * of the string and then using total, output each character from highest
  44.  * subscript element to lowest, outputting the character in reverse.
  45.  ******************************************************************************/
  46. void reverse(char *a)
  47. {
  48. int charnumber = strlen(a); //Stores the string length to variable
  49. cout << "\nWord Reversed: ";
  50. //
  51. //Initializes for loop with the limit of the string length
  52. for(int count = charnumber - 1; count >= 0; count--)
  53. cout << a[count]; //Outputs each character in reverse
  54. }
Success #stdin #stdout 0s 544KB
stdin
 pneumonoultramicroscopicsilicovolcanoconiosis
stdout
Enter a Word: 
Word Reversed: sisoinoconaclovociliscipocsorcimartluonomuenp