You are on page 1of 1

/* Question One [50 marks]

Write a complete C++ program that asks the user to enter a character string. Send
the string to a function called ReverseIt. This function will fill a second string
so that the original string is reversed. Limit the size of the strings to fifty
characters. The last character in the original string (before the null) should be
the first character of the second string. Incorporate a loop so that the user can
continue to enter strings until he or she chooses to stop. */

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void ReverseIt(char stringg[50]);

int main(){
char stringg[50];
bool loop = true;

while(loop){

cout<< "Enter a word to reverse: ";


cin.getline(stringg, 50, '\n');
cout<< "Original word: "<< stringg << " "<< "Reversed word: ";
ReverseIt(stringg);

string ans;
cout << "\nDo you want to reverse another word? (yes/no) : " ;
cin >> ans;

if(ans == "yes"){
loop = true;
cout<< endl;
}
else{
loop = false;
}
}

cout<< endl;
return 0;
}

void ReverseIt(char stringg[50]){


char reverse[50];
int length = strlen(stringg);

for (int i =0; i<=length; i++){


reverse[i] = stringg[length-(i + 1)]; // lets say [0] = [8-1] ->
beacuse array starts from 0

}
cout<< reverse;
}

You might also like