76.7k views
23 votes
Implement one array of the English alphabet (26 characters). a) Create a function, getLetters() to cast and generate the array. b) Create a function, printLetters() to output the array. c) Create a function, swap() for swapping character variables. d) Create a function, reverseLetters() uses a loop and the swap() to reverse all array elements. e) Call printLetters()to Output the updated array. Hint: Set a variable for the first and last indices. Swap those values. Gradually move the first/last pointers to the middle of the array, swapping as you go. When the middle is reached, the array will be reversed.

User HansUp
by
5.0k points

1 Answer

5 votes

#include <fstream>

#include <iostream>

#include <iomanip>

#include <cstring>

#include <cstdlib>

using namespace std;

// Function Declarations

void display(char alphabets[],int MAX_SIZE);

void reverse(char alphabets[],int MAX_SIZE);

void swap(char &ch1,char &ch2);

int main() {

//Declaring constant

const int MAX_SIZE=26;

//Declaring a char array

char alphabets[MAX_SIZE];

//Populating the array with alphabets

for(int i=0;i<MAX_SIZE;i++)

{

alphabets[i]=(char)(65+i);

}

cout<<"Original: ";

display(alphabets,MAX_SIZE);

reverse(alphabets,MAX_SIZE);

cout<<"Reversed: ";

display(alphabets,MAX_SIZE);

return 0;

}

//This function will display the array contents

void display(char alphabets[],int MAX_SIZE)

{

for(int i=0;i<MAX_SIZE;i++)

{

cout<<alphabets[i]<<" ";

}

cout<<endl;

}

//This function will reverse the array elements

void reverse(char alphabets[],int MAX_SIZE)

{

int first,last;

first=0;

last=MAX_SIZE-1;

while(first<last)

{

swap(alphabets[first],alphabets[last]);

first++;

last--;

}

}

void swap(char &ch1,char &ch2)

{

char temp;

temp=ch1;

ch1=ch2;

ch2=temp;

User Pheon
by
4.6k points