SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Arrays
Exercise:
Self-test Exercises
Question:18 | ISBN:9780321531346 | Edition: 7

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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 

 

0 0

Discussions

Post the discussion to improve the above solution.