Reversing a c-style string Through Pointers

Reversing a c-style string Through Pointers

Page 1 of 1

10 Replies - 6726 Views - Last Post: 11 November 2007 - 12:07 AM Rate Topic: ***** 1 Votes

#1 HermosaBeach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-August 07

Reversing a c-style string Through Pointers

Post icon  Posted 30 August 2007 - 11:06 AM

I am trying to get better with pointers by Reversing a c-style string. What I have done is pass a pointer to buffer to my reverse function, I want to get the address of that pointer, then grab the word to be reversed. I realize theres a built in function for reversing , but i do not want to use it . I want to access all elements of the array using pointers, reverse the array by accessing the last part of the array with a pointer and being able to pass it back to the main function using a void function. Does this make sense? I need to reverse the c-style string without touching anything that I have done in int main() so far, I feel I just need help with the reverse function thanks!


#include <iostream>
using namespace std;

void reverse(char*);

int main(){
	char buffer[80];
	cout << "\nString reversal program";
	cout << "\nType in a short line of words";
	cout << "\nI will reverse them";
	cout << "\n";
	cin.getline(buffer,79);
	
	cout << "\nYou typed " << buffer;
	reverse(buffer);
	cout << "\nReversed: " << buffer;
	
	cout << endl;
	system("PAUSE");
	return 0;
}


void reverse(char* string){
  
	int length;
	length = strlen(string);

// i dont know where to go from here....
		
}



Is This A Good Question/Topic? 0
  • +

Replies To: Reversing a c-style string Through Pointers

#2 HermosaBeach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-August 07

Re: Reversing a c-style string Through Pointers

Posted 30 August 2007 - 11:24 AM

I looked at the other posts and they reverse strings, But not the way I am trying to accomplish in my code. I kind of dont want to use recursion or anything just yet , just simply use pointers and for loops if need be, but i keep getting errors.
Was This Post Helpful? 0
  • +
  • -

#3 HermosaBeach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-August 07

Re: Reversing a c-style string Through Pointers

Posted 30 August 2007 - 12:19 PM

Ok Im still working on this , Heres what I have for the function so far

void reverse(char* string){
	
		int length;
	length = strlen(string)-1;
	
	
	char temp[length];
	
//trying to swap from reading from end of array first
	for(int y =length; y < length; y--)
	temp[y] = string[y];

//putting the new reversed array back into string....
 for(int i = 0; i < strlen(string);i++)
		string[i] = temp[i];
	
	
	
}




CAN SOMEONE PLEASE TELL ME WHAT IM DOING WRONG HERE!!!! :angry:

This post has been edited by HermosaBeach: 30 August 2007 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Reversing a c-style string Through Pointers

Posted 30 August 2007 - 12:23 PM

You are assigning items to the end of array names temp...then trying to load items from the beginning of that array to string. Also:
for(int y =length; y < length; y--)


will break the loop after the first iteration, as y will then be less than length.

Here is an extremely brute force, non optimized version of what you wish to do...it shows the idea, but can be done much more efficiently.

#include <iostream>
#include <cstring>

using namespace std;

void reverse(char *str1);

int main(int argc, char *argv[])
{
	char buffer[80];
	cin.getline(buffer,80);
	reverse(buffer);
	cout<<"\n"<<buffer<<endl;
	return 0;
}

void reverse(char *str1)
{
	 char tmp[80];
	 int counter=0;
	 for(int i=(strlen(str1)-1);i>=0;i--)
	 {
		   tmp[counter]=str1[i];
		   counter++;
	 }  
	 for(int i=0;i<strlen(str1);i++)
	 {
		 str1[i]=tmp[i];
	 }
}


Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Reversing a c-style string Through Pointers

Posted 30 August 2007 - 12:47 PM

Perhaps consider this way of doing things -

imagine an array like this - [ 'P', 's', 'e', 'u', 'd', 'o', 'c', 'o', 'd', 'e', '\0' ]

The array is 11 characters long, although, the final null-terminating character needs to be left in-place in order to maintain it as a 10-character string.

so, the reversable portion of the array is indexed from 0-9.
[ 'P', 's', 'e', 'u', 'd', 'o', 'c', 'o', 'd', 'e' ]

In order to reverse it, the easiest way is - (Using bold to show pointers to 'first' and 'last' characters)

Swap the first & last characters in-place.
[ 'e', 's', 'e', 'u', 'd', 'o', 'c', 'o', 'd', 'P' ]

then narrow the bounds of the reversable array, to indices 1-8
[ 's', 'e', 'u', 'd', 'o', 'c', 'o', 'd' ]

then again, swap the first and last characters in-place.
[ 'd', 'e', 'u', 'd', 'o', 'c', 'o', 's' ]

then narrow the bounds of the reversable array to indices 2-7
[ 'e', 'u', 'd', 'o', 'c', 'o' ]

then again, swap the first and last characters in-place.
[ 'o', 'u', 'd', 'o', 'c', 'e' ]

then narrow the bounds of the reversable array to indices 3-6
[ 'u', 'd', 'o', 'c' ]

etc..


All the time, you'll keep a track of where the string begins, this "placeholder" (pointer) won't change - the complete string still starts at position 0.

After going through your string, it should end up like this
[ 'e', 'd', 'o', 'c', 'o', 'd', 'u', 'e', 's', 'P', '\0' ]

If you make yourself a simple swap function, you can modify the string in-place using your temporary pointers 'first' and 'last'.
void swap( char* a, char* b);



edited to remove smileys..

This post has been edited by Bench: 30 August 2007 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#6 HermosaBeach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-August 07

Re: Reversing a c-style string Through Pointers

Posted 30 August 2007 - 01:02 PM

This May sound like such a noobish question, but why do we need the -1 after getting the length of the string again?

f for(int i= strlen(string)-1; i >= 0; --i)


for it to work ?


are we moving -1 away from the null terminating char?

This post has been edited by HermosaBeach: 30 August 2007 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Reversing a c-style string Through Pointers

Posted 30 August 2007 - 01:10 PM

View PostHermosaBeach, on 30 Aug, 2007 - 09:02 PM, said:

are we moving -1 away from the null terminating char?

Yes, because the null terminating char denotes end-of-string. If the first character in your string is '\0' (nul) then your string is effectively empty, regardless of what the other elements in your array are.

- The reason the null-terminator is required, is that the size of an array is typically not known when its passed to a function (Due to the way that pointers and array-passing work). So the only way to track the length of a string, is to iterate through the array and look for the null-terminating character. This is how all the various I/O functions and string manipulation functions are able to represent raw character arrays properly. The alternative would be for the user of those functions to always keep a track of the string's length, which is undesirable, for obvious reasons.

if you use a string library such as the C++ std::string, then things generally work differently, and you don't need to worry about null terminators (Or, at least, the library will take care of this side of things for you)

This post has been edited by Bench: 30 August 2007 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 HermosaBeach  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-August 07

Re: Reversing a c-style string Through Pointers

Posted 30 August 2007 - 01:22 PM

Awesome! Thanks bench and Amadeus for the help . I'm going to work on this a little bit more until I get a good grasp of it thanks.
Was This Post Helpful? 0
  • +
  • -

#9 kingsalami  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 07

Re: Reversing a c-style string Through Pointers

Posted 09 November 2007 - 02:12 PM

-using strlen or any length function is an immediate sign of weakness.

-creating an array is not a solution, it limits you to a certain size. That sucks.

-this is essentially a pointer arithmatic question.

-elegance and brevity should be strived for.

-I think recursion here is a poor solution, as you use more memory and cpu cycles by throwing function calls on the stack and then unwinding the stack to reverse.

-this function prints the reversed string. Any thing can be substituted here instead of a cout, you could store it etc...

void Reverse(char* str)
{
	char* p = str;

	while(*p != '\0') ++p;

	do
	{
		--p;
		cout << *p;
	}while(p != str);
}



If the code doesn't make sense to you all it does is creates a pointer that moves to the end of the string and then prints the chars one at a time from the end back to the front until the memory locations are the same and then it stops.
Was This Post Helpful? 0
  • +
  • -

#10 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Reversing a c-style string Through Pointers

Posted 09 November 2007 - 02:55 PM

I'm just going to refer you to the comments made on the other, identical copy of this post that you just made:
http://www.dreaminco...wtopic32472.htm
Was This Post Helpful? 0
  • +
  • -

#11 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Reversing a c-style string Through Pointers

Posted 11 November 2007 - 12:07 AM

I guess Bench already mentioned what I was about to say.
I'd recommend that method since it cuts down the number of loop cycles by half.
That's saves a lot of time for reversing longer strings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1