Reverse string with Pointers

I got the gist of the project, but it seems as if I am missing something simple. Basically, I am trying to reverse a set a characters that a user input. Now I built the program using pointers. the *head is suppose to point at the beginning of the string and *tail points at the end (not the '\0'). I originally wasn't getting any output after compiling, but after making some changes and realize that I can get rid of the 2nd string I had and use one, but increment the head and decrement the tail and output the tail. So then I made a temp variable so that it can start the switching. Now after compiling, all I get is m (i am testing the word 'murder') Could someone tell me where I am going wrong with my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

#include <cstring>

int main( )
{
	char input[256]; // inputReverse[256]; //user input, 256 characters should be sufficient
	char *head, *tail;
	char temp; // temporary variable
	int i = 0; //used to assign temporary variable

	cout << "Please enter a word or characters (no spaces) : ";
	cin >> input;

	head = input + strlen(input) - 1;

	tail = input + 1;

	while (tail <= input){
	temp = input[i];
	input[i] = *tail;
	*tail = temp;
	i++;
	*head++;
	*tail--;
	
	}
	

	*tail = '\0';

	cout << "The reverse is: " << input;
	cout << endl;

	return 0;
}
You can use interators on a standard string. A string is container-like. Or you can reverse it with the standard reverse function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
        std::string str = "kbw";

        // forward
        for (std::string::const_iterator p = str.begin(); p != str.end(); ++p)
                std::cout << *p;
        std::cout << std::endl;

        // backward
        for (std::string::const_reverse_iterator p = str.rbegin(); p != str.rend(); ++p)
                std::cout << *p;
        std::cout << std::endl;

        // reverse
        std::string rstr = str;
        std::reverse(rstr.begin(), rstr.end());
        std::cout << rstr << std::endl;

        return 0;
}



As far as your code goes, it's conventional to start with head = input, tail = input + strlen(input)
Last edited on
What I would do (untested, and copied from yours):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

#include <cstring>

int main( )
{
	char input[256]; // inputReverse[256]; //user input, 256 characters should be sufficient
	int i = 0; //used to assign temporary variable

	cout << "Please enter a word or characters (no spaces) : ";
	cin >> input;

	char * result = new char[strlen(input) + 1];
	char *navigator = input + strlen(input);
	char * writer = result;
	while(navigator != input)
	{
		navigator--;
		*writer = *navigator;
		writer++;
	}

	*writer = '\0';
	cout << "The reverse is: " << result;
	cout << endl;

	return 0;
}
Thanks for the help. I figured the assigning the temporary variable. After an hour it started to reverse, but would only output the one letter. So I kept re adjusting and it works. Thanks!
Topic archived. No new replies allowed.