Write a program that will read up to ten letters into an array and write the
letters back to the screen in the reverse order. For example, if the input is
abcd. then the output should be dcba
Use a period as a sentinel value to mark the end of the input. Call the
array letter_box. For this exercise you need not use any functions. This is
just a toy program and can be very minimal.
C++ Program Code:
#include <iostream>
using namespace std;
//Define constant size
const int SIZE = 10;
int main ( )
{
char letters[SIZE], input;
int indexVal = 0;
int revLetters;
//Read up to ten letters into an array
cout << "Enter any ten letters : \n";
cin >> input;
while ( (input != '.') && (indexVal < SIZE) )
{
letters[indexVal] = input;
indexVal++;
cin >> input;
}
revLetters = indexVal;
//Display the letters back to the screen
cout << "Letters revers order of given array: \n";
for (indexVal = revLetters - 1; indexVal >= 0; indexVal--)
cout << letters[indexVal];
cout << endl;
return 0;
}
Output of the program code:
Enter any ten letters :
HelloPeter
Letters revers order of given array:
retePolleH