#include <iostream>
#include <cstring>
usingnamespace std;
void get_input(char *char_ptr, int MAX);
void reverse_string(char *char_ptr);
void display_result(char *char_ptr);
void print_address(char *char_ptr, constint &MAX);
bool repeat_program();
int main()
{
constint MAX = 50;
char user_entry[MAX];
char* char_ptr;
bool repeat;
char_ptr = user_entry;
do{
get_input(char_ptr, MAX);
reverse_string(char_ptr);
display_result(char_ptr);
print_address(char_ptr, MAX);
repeat = repeat_program();
}while (repeat == true);
return 0;
}
void get_input(char *char_ptr, int MAX)
{
cout << "Please enter a string" << endl;
cin.getline(char_ptr, MAX + 1);
cout <<"The string before reversing: " << *char_ptr << endl;
}
void reverse_string(char *char_ptr)
{
char *front, *back, temp;
front = char_ptr;
back = char_ptr + strlen(char_ptr) - 1;
while (front < back){
temp = *front;
*front = *back;
*back = temp;
front ++;
back --;
}
}
void display_result(char *char_ptr)
{
cout << "The string after reversing: ";
for (int i = 0; *(char_ptr + i) != '\0'; i++)
{
cout << *(char_ptr + i);
}
cout << endl;
}
void print_address(char *char_ptr, constint& MAX)
{
cout <<"The address of variables \"char_ptr\" is: " << (long)char_ptr << endl;
cout <<(long)&char_ptr<<endl;
}
bool repeat_program()
{
char repeat;
cout << "Would you like to go again? Y/y for yes, any other char for no" << endl;
cin >> repeat;
cin.ignore(1000, '\n');
if (toupper(repeat) == 'Y')
returntrue;
elsereturnfalse;
}
Sameple Dialogue:
Please enter a string
sdfdsf
The string before reversing: s
The string after reversing: fsdfds
The address of variables "char_ptr" is: -239338872
-239338928
Would you like to go again? Y/y for yes, any other char for no