Reverseing a string using pointers

Yes this i an assingment for my C++ class. I just need a little help figuring out how to point to the end of a string. Basically we are suppose to create a char pointer that can be initialized to whatever. We can't use CString or String class or arrays. I can't figure out how to get the length of the string with out using strlen.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	int  i;
	char *string;
	string = "John Doe";

	cout << "\nName is: " << string; 
	
	

	system("pause");
	return 0;
}

this is what i have. I know that if i use *(string + 0) that will point to the first char in the string but how do i get it to point to the last char in the string. From the instruction John Doe could be changed to Sam and that would have to be printed in reverse. I don't want the whole program just some advice on the pointer part or maybe a link to something that would give a better clue then the tutorial here.
Last edited on
Well c-strings are null terminated, which means they end with a '\0' character (thats backslash zero.)

Also remember that c-strings are just char[], so:
"" == { '\0' }
"Hello" == { 'H', 'e', 'l', 'l', 'o', '\0' }
Last edited on
yep i just figured that part out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	int i = 0;
	int x = 0;
	char *string;
	string = "John Doe";

	cout << "\nName is: " << string << endl; 
	while (*(string + i) != '\0')
		i++;

	cout << i <<  endl;
	

	system("pause");
	return 0;
}

i = 8 now i just have to point to the next to last char and print it. Thanx
Your welcome.
This is what i have got as almost finished product, except, it won't do the last for statement. cout << i << *(string + i) << endl; prints 7 and e for and using breakpoints on line 14 all the i's are 7, even the ones in the last for statement. Can't figured out why it won't do the last for statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	int i = 0;
	int x = 0;
	char *string;
	string = "John Doe";

	cout << "\nName is: " << string << endl; 
	
	while (*(string + i) != '\0')
		i++;
	cout << i << endl;
	i-=1;
	cout << i << *(string + i) << endl;

	for (i; i <= 0; i--)
		cout << "The name backwards is : " << *(string + i) << endl;
	
	system("pause");
	return 0;
Last edited on
NVM i got it the < should have been a >
okay last question how do i assign the value of *(string + i) to another string variable in my last for statement. This is what i have and the for statement works to decrement i but not for assigning.
1
2
3
4
5
6
7
for (i-=1; i >= 0; i--)
	{
		*(newstring + x) = *(string + i);
		x++;
	}
	cout << "The name backwards is : " << string << endl;
	 

Last edited on
This is my finished product but the part from line 12 - 16 seems like cheating to me. How can i assign the value of string + 1 to another string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	int i = 0;
	char *string;
	string = "John Doe";

	cout << "\nName is: " << string << endl; 
	
	while (*(string + i) != '\0')
		i++;
	
	cout << "The name backwards is: ";
	for (i-=1; i >= 0; i--)
		cout << *(string + i);
	cout << endl;
		
	system("pause");
	return 0;
}
You must allocate enough space for newstring.

you cannot have two loops in the same function that decrements i. After the first loop the size of the string is gone.
Last edited on
You need to store the length somewhere (say n) and then assign the indexes of the backward string as such:
1
2
3
4
backward_string[0] = forward_string[n - 1]
backward_string[1] = forward_string[n - 2]
...
backward_string[n - 1] = forward_string[0]

so something like:
 
backward_string[i] = forward_string[n - 1 - i] //as i loops from 0 to n - 1 
Last edited on
Thanx Mathhead but my teacher specifically said we can't use arrays. Now i have a working version of the program it just seems a little cheap to me. In the code i have in this post i would like to know how to make my for statement work. Every time i run it i get an assignment error. I personally don't see how to make it work without using an array but I'm very new to this.
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
int main()
{
	int i = 0;
	int x = 0;
	char *string, *newstring;
	string = "John Doe";
	newstring = "                                                                     ";

	cout << "\nName is: " << string << endl; 
	
	while (*(string + i) != '\0')
		i++;
	cout << i << endl;
	i-=1;
	cout << i << *(string + i) << endl;

	for (i-=1; i >= 0; i--)
	{
		*(newstring + x) = *(string + i);
		x++;
	}
	cout << "The name backwards is : " << string << endl;

	
	system("pause");
	return 0;
}
Erm, maybe your teacher doesn't realize this but:

*(string + i); is the same thing as: string[i];

Just program it using "arrays" and then go through and perform the switch above if your teacher requires to use that stupid format for some reason.
Yea, a c-string is an array (const char* == char[])
There is no way to get around that...

If you wanted to store this c-string in memory without a traditional array, you will need to define your own linked-list-style class...? I doubt that's what you want.

And yea the [] operator is just the offset operator, it offsets the pointer (i.e. pointer + offset) then dereferences it!
By the way... Normally you would avoid line 7
newstring = /* a very long string */ ;
by dynamically allocating an array, and storing it to that pointer.
newstring = new char[n]; //where n = length of string
(but I guess that uses arrays too...)
Last edited on
Thanks for your guy's help. I think the teacher was just wanting to emphasize the point that *(string + i); is the same thing as: string[i]. Again thanks.
Topic archived. No new replies allowed.