I'm attempting to write a simple program that will reverse a users input utilizing pointers. This is my first time working with pointers and in theory my program seems like it would work: have an array, write the users input to the array, point one pointer to the head and the other to the end and have the while loop do the rest. However, my program isn't working properly. My question is, what exactly am I doing wrong?
Heres my code:
#include <iostream>
#include <string>
using namespace std;
int main() {
char user_input[1000] = " ";
cout << "Enter a word to be reversed: " << endl;
cin >> user_input;
int myChar = sizeof(user_input) - 1;
char *start = user_input;
char *end = user_input + myChar - 1;
while (start < end) {
char save = *start;
*start = *end;
*end = save;
start++;
end--;
}
cout << user_input;
}
And my output:
Enter a word to be reversed:
hello <--- my input
<--- no output
My question is, what exactly am I doing wrong?You're not using STL,std::stringandstd::reverse