0

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
5
  • 2
    My question is, what exactly am I doing wrong? You're not using STL, std::string and std::reverse Commented Nov 3, 2015 at 13:32
  • possible duplicate to: stackoverflow.com/questions/10302524/… Commented Nov 3, 2015 at 13:44
  • And what happens if I enter a word with more than 999 characters? Commented Nov 3, 2015 at 15:20
  • @ChristianHackl - where would you find one? ;) Commented Nov 3, 2015 at 16:54
  • 1
    @owacoder: German grammar allows compounds of infinite length. Let's see... "Computerproblemlösungskompetenzseminarlehrergehaltsverhandlungspausengrund." - Reason for pausing negotations about the salary of seminar teachers for competence in solving computer problems. You can extend this at will and even make it recursive. (But before you go on and do so, mind that even though the grammar allows it, good style is a different story :)) Commented Nov 3, 2015 at 17:03

4 Answers 4

5

The line

int myChar = sizeof(user_input) - 1;

should be

#include <string.h>

int myChar = strlen(user_input);

Currently, you are reversing all 1000 characters in your array. The characters beyond the end of your inputted string are not initialized, so you should only reverse the number of characters the user input. strlen() finds the length for you.

Another alternative: use the standard library.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I missed strlen() in my studies.
3

sizeof(user_input) is always 1000 since user_input is an array of 1000 1-byte elements.

You need to use strlen instead. It returns the index of the terminating null character.

Comments

2

declare user_input as char *user_input=new char[1000]


so what is the problem ??


well the problem is when you take input from user in user_input it take it as user_input[0] so either run a for loop from 0 to n-1 or use my way given above moreover output is still errorneous that i will leave for u .....

1 Comment

Thank you! I know understand better.
0

You can also write a strlen function by yourself.
The end of the user's input would be the index of the first 0 as an int or '\0' AS a char.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.