Another string reversal, but with pointers

need help getting pointers to work

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 3203 Views - Last Post: 02 December 2008 - 07:49 AM Rate Topic: -----

#1 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Another string reversal, but with pointers

Post icon  Posted 01 December 2008 - 12:08 PM

I have a working program to reverse a given string, and need to update it to use pointers for the assignment requirements.

With the way I have it set up now I am getting an invalid conversion from char to string, I've changed everything to cstring arrays and get an invalid conversion from char to char error message. Now that's funny!

So I may be using the wrong type for the pointers, or the wrong method of passing the string to the function I'm not sure.

If you look down the code to the ptr1 and ptr2 pointers I am attempting to create, I want to switch out the start and end int variables with the pointers of ptr1 and ptr2. and do the exact same string reversal.

I would appreciate any and all suggestions. Also you can ignore the return temp line, I'm not using it right now I know but I'm not sure if I'm going to print the final line in main or the sub function yet so I left it in there for now.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string switchit(string& s);

int main()
{
	string stuff1 = "this is stuff";
	string stuff2 = "racecar";
	string stuff3 = "this is a test dude";
	string stuff4 = "1234567";	
	
	
	cout << "\nThis is original string 1: \n";
	cout << stuff1 << "\n\n";
	switchit(stuff1);	
	cout << "\nThis is original string 2: \n";
	cout << stuff2 << "\n\n";
	switchit(stuff2);
	cout << "\nThis is original string 3: \n";
	cout << stuff3 << "\n\n";
	switchit(stuff3);	
	cout << "\nThis is original string 4: \n";
	cout << stuff4 << "\n\n";
	switchit(stuff4);


	system("PAUSE");
	return EXIT_SUCCESS;
}

string switchit(string& s)
{
//	char *ptr1;
//	int  *ptr1;  //these methods all had errors also

	
	int start = 0;
	int end = s.length();
 
	
	string *ptr1;
	string *ptr2;
	ptr1 = s[0];  //errors out on compile invalid char/string conversion
	ptr2 = s[end];//errors out on compile invalid char/string conversion
	
	string temp(s);
	
	while(start < end)
	//while ptr1<ptr2)
	{
	   end--; //want to switch out for ptr1
	   //ptr2--;
	   swap(temp[start],temp[end]);
	   //swap(ptr1,ptr2); //this is what I need to do, swap the pointers
	   start++;
	   //ptr1++;
	}
	
	cout << "\nThis is the modified string: \n";
	cout << temp << "\n\n";
	
	return temp;
}


This post has been edited by badjava: 01 December 2008 - 12:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Another string reversal, but with pointers

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 12:35 PM

Quick clarification, what style do the need to be "swapped" strings need to be? C or C++ style strings. It's important because I could have a solution that goes either way.
Was This Post Helpful? 0
  • +
  • -

#3 Evomach1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 12:56 PM

View PostKYA, on 1 Dec, 2008 - 11:35 AM, said:

Quick clarification, what style do the need to be "swapped" strings need to be? C or C++ style strings. It's important because I could have a solution that goes either way.



I think it's for C++
Was This Post Helpful? 0
  • +
  • -

#4 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 01:19 PM

View PostKYA, on 1 Dec, 2008 - 11:35 AM, said:

Quick clarification, what style do the need to be "swapped" strings need to be? C or C++ style strings. It's important because I could have a solution that goes either way.



If I understand what you mean and responding to the next poster after this, it is c++ and it can be done with the c++ string class or the cstrings (char right?) either way. Personally I prefer the string class since it seems to just make more sense to me intuitively. I'm open to either tho if one is significantly more simple for this application!
Was This Post Helpful? 0
  • +
  • -

#5 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 01:20 PM

View PostEvomach1, on 1 Dec, 2008 - 01:56 PM, said:

View PostKYA, on 1 Dec, 2008 - 11:35 AM, said:

Quick clarification, what style do the need to be "swapped" strings need to be? C or C++ style strings. It's important because I could have a solution that goes either way.



I think it's for C++


It's C++, his errors are from trying to use C style handleing on C++ string objects.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 01:22 PM

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void switchit(string& s);

int main()
{
    string stuff1 = "this is stuff";
    string stuff2 = "racecar";
    string stuff3 = "this is a test dude";
    string stuff4 = "1234567";    
    
    
    cout << "\nThis is original string 1: \n";
    cout << stuff1 << "\n\n";
    switchit(stuff1);  
      cout << endl;  
       cout << stuff1 << "\n\n";
    cout << "\nThis is original string 2: \n";
    cout << stuff2 << "\n\n";
    switchit(stuff2);
    cout << stuff2 << "\n\n";
      cout << endl;
    cout << "\nThis is original string 3: \n";
    cout << stuff3 << "\n\n";
    switchit(stuff3);  
    cout << stuff3 << "\n\n"; 
    cout << endl; 
    cout << "\nThis is original string 4: \n";
    cout << stuff4 << "\n\n";
    switchit(stuff4);
    cout << stuff4 << "\n\n";


    system("PAUSE");
    return EXIT_SUCCESS;
}
void switchit(string& s)
{
       //allocate memory and get string values
       char* temp = new char[s.size()+1];
       for(int i = 0; i < s.size(); i++)
       {
               temp[i] = s.at(i);
       }
       temp[s.size()] = '\0';
       
       char swap;
       //swap it
       for (int j = s.size()-1, i = 0; j > s.size()/2 ; j--, i++)
       {
           swap = temp[i];
           temp[i] = temp[j];
           temp[j] = swap;
       }
       //reassign switched to string reference
       for(int i = 0; i < s.size(); i++)
       {
               s.at(i) = temp[i];
       }
       
       
       delete [] temp;
}



edited to show the whole example. :)

This post has been edited by KYA: 01 December 2008 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 02:24 PM

I haven't analysed this carefully but won't this cause an OBOE (Off By One Error):
while(start < end)



When start reaches end don't you want it to do work on that last character in the string?

Apologies if I'm missing the point or making a blunder.
Was This Post Helpful? 0
  • +
  • -

#8 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 02:27 PM

View PostKYA, on 1 Dec, 2008 - 12:22 PM, said:

void switchit(string& s)
{
       //allocate memory and get string values
       char* temp = new char[s.size()+1];
       for(int i = 0; i < s.size(); i++)
       {
               temp[i] = s.at(i);
       }
       temp[s.size()] = '\0';
       
       char swap;
       //swap it
       for (int j = s.size()-1, i = 0; j > s.size()/2 ; j--, i++)
       {
           swap = temp[i];
           temp[i] = temp[j];
           temp[j] = swap;
       }
       //reassign switched to string reference
       for(int i = 0; i < s.size(); i++)
       {
               s.at(i) = temp[i];
       }
       
       
       delete [] temp;
}



edited to show the whole example. :)


Wow, I have never seen anything like that for loop, it's almost looks like a nested for loop right within the original for loop framework.

Thank you for the full edit, I'm chewing this over right now.

The one solid requirement for the assignment is that I have to use a front and end pointer on the incoming string, swap the two pointers and reverse the string by incrementing and decrementing the newly swapped pointers until the original string is reversified.

I'm not sure yet as I decipher this method but I don't think it is using the fore and aft pointers, am I correct? Your solution is probably a lot more on the elegant side where my requirements are more on the plodding methodical grinding side :)

This post has been edited by badjava: 01 December 2008 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 02:48 PM

No my current solution does not use a front and end pointer. Let me make some modifications and see what I come up with. I think my version is more elegant, but I fully understand academic constraints, I have many myself

Two pointers is more prone to errors, but if its what the teacher wants...Half of college has been figuring out all the instructors quirks :)

edited for a typo

This post has been edited by KYA: 01 December 2008 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#10 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 03:20 PM

View Postjanotte, on 1 Dec, 2008 - 03:24 PM, said:

I haven't analysed this carefully but won't this cause an OBOE (Off By One Error):
while(start < end)



In java's code, yes, it wont work, the end-- and start++ are out of sync. For what he is atempting, no, it will work fine.

For the string : string
start 0, end 5 : gtrins
start 1, end 4 : gnrits
start 2, end 3 : gnirts
start 3, end 2 : exit loop, start is too big

For the string : seven
start 0, end 4 : neves
start 1, end 3 : neves
start 2, end 2 : exit loop, start is too big

Ya, ok, seven is not the best example.

View Postjanotte, on 1 Dec, 2008 - 03:24 PM, said:

Apologies if I'm missing the point or making a blunder.


Don't worry about it. I do it all the time.
Was This Post Helpful? 0
  • +
  • -

#11 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 03:29 PM

View Postbrds, on 1 Dec, 2008 - 02:20 PM, said:

View Postjanotte, on 1 Dec, 2008 - 03:24 PM, said:

I haven't analysed this carefully but won't this cause an OBOE (Off By One Error):
while(start < end)



In java's code, yes, it wont work, the end-- and start++ are out of sync. For what he is atempting, no, it will work fine.

For the string : string
start 0, end 5 : gtrins
start 1, end 4 : gnrits
start 2, end 3 : gnirts
start 3, end 2 : exit loop, start is too big

For the string : seven
start 0, end 4 : neves
start 1, end 3 : neves
start 2, end 2 : exit loop, start is too big

Ya, ok, seven is not the best example.


I had the same basic concern as janotte and had to prove it to myself on paper walking through one element at a time like brds did. My brain kept telling me no way dude when u hit the middle 2 elements on an even string your gonna b in trouble. Turns out after walking through on paper the odd sized string is the weirder one but it still pulls through. The way I visualize it I don't like it, but my paper says it's a go :P
Was This Post Helpful? 0
  • +
  • -

#12 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 04:13 PM

[edit] this didnt quite get me as far as i expected, this got me a pointer to a character in the array but doesnt let me swap the two necessarily right? my while loop will need to still reference an element counter, and the element character pointer... I think i hate my prof[/edit]

AH HA! I think I just made a break through.

I have been able to set a pointer to the function passed array, they both act like pointers and they like to play well together right?

It was killing me trying to set the second pointer to the end of the incoming string getting an error of invalid conversion over and over no matter what type of string or pointers I used.

Then I tried this:
	 int end = 0;
	end = strlen(s);
	char *ptr2;
	ptr2 = new char;
	ptr2 = &s[end-1];
	cout << "\nptr2 = s[end] which prints: " << ptr2 << "\n";   



I'm still trying to understand why this works and let me finally push that second pointer out to an array element rather than just point to the beginning address of the array.

If anyone could put this in laymans terms for me and explain why it is working I will be like for ever in your eternal debt cause holy crap! lol this was a mind bender for me to figure out the first time around.

Our text book is just awful and only gives 'int' type examples 99% of the time, almost no char or string examples at all, it's about worth it's weight in mud.

This post has been edited by badjava: 01 December 2008 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 04:37 PM

Thanks heaps brds and badjava.

The best learnings are when I'm proved wrong.
Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 04:47 PM

Here's another go at it using front and end pointers:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

void switchit(string& s);

int main()
{
    string stuff1 = "this is stuff";
    string stuff2 = "racecar";
    string stuff3 = "this is a test dude";
    string stuff4 = "1234567";    
    
    
    cout << "\nThis is original string 1: \n";
    cout << stuff1 << "\n\n";
    switchit(stuff1);  
    cout << endl;  
    cout << stuff1 << "\n\n";
    cout << "\nThis is original string 2: \n";
    cout << stuff2 << "\n\n";
    switchit(stuff2);
    cout << stuff2 << "\n\n";
    cout << endl;
    cout << "\nThis is original string 3: \n";
    cout << stuff3 << "\n\n";
    switchit(stuff3);  
    cout << stuff3 << "\n\n"; 
    cout << endl; 
    cout << "\nThis is original string 4: \n";
    cout << stuff4 << "\n\n";
    switchit(stuff4);
    cout << stuff4 << "\n\n";


    system("PAUSE");
    return EXIT_SUCCESS;
}

void switchit(string& s)
{
     char* front, *back;
	 char* temp = new char[s.size()+1];
     
     for(int i = s.size()-1, j = 0; i > 0; i--, j++)
     {
         //assignment
         front = &s.at(j);
         back = &s.at(i);
		 temp[i]= *front;
		 temp[j] = *back;
     }
	 temp[s.size()] = '\0'; //apend null terminator
     //no nasty dangling pointers
     front = 0;
     back = 0;

     for(int i = 0; i < s.size(); i++)
     {
			s.at(i) = temp[i];
     }
	 delete [] temp;
 }  



Still uses a dynamically allocated array for storage purposes.


edited to include all the code again and to correct some indentation

This post has been edited by KYA: 01 December 2008 - 04:48 PM

Was This Post Helpful? 1
  • +
  • -

#15 badjava  Icon User is offline

  • Lux Ex Tenebris
  • member icon

Reputation: 14
  • View blog
  • Posts: 540
  • Joined: 30-October 08

Re: Another string reversal, but with pointers

Posted 01 December 2008 - 07:53 PM

View Postjanotte, on 1 Dec, 2008 - 03:37 PM, said:

Thanks heaps brds and badjava.

The best learnings are when I'm proved wrong.


Oh I didn't prove anyone wrong I'm just kind of buzzing along like a blind bumblebee :) I've wrenched and wrenched on my code to get something that spits out the right output and turned it in only to have the prof. tell me 'the code you have shouldn't be able to do that...but good job' lol! And it wasn't me trying for any obfuscated code award lemme tell u. I had only walked this one through on paper about 15 minutes before your post otherwise I would have still completely agreed with you :P

View PostKYA, on 1 Dec, 2008 - 03:47 PM, said:

Still uses a dynamically allocated array for storage purposes.

edited to include all the code again and to correct some indentation


Oh that is nice code KYA, ty for the example, I think I even understand everything you have in there!! OMG mebbe I'm learning some programming *gasp* lol.

I was thrashing through the code after my last post and realized I would need another temp string and the index to step through for saving the swapped letters but I was still fighting with my original while() loop. I think your for loop really makes things clean. I hadn't yet reached any way to produce those char front and back pointers so I really appreciate the help.

I also really don't think there is a complex for loop example like your code in my entire text book, and that's kind of sad isn't it??

If anyone gets through my rambling this far I have a question for about the 'c style' strings/poiners mentioned a few posts back. Is this part of my code:
char *ptr2;
considered the old 'c style' code? And something like this:
ptr2 = new char;
ptr2 = &s[end-1];

or better yet:
front = &s.at(j);
is considered to be the newer 'c++ style' strings?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2