Thread: New and improved pointer question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    New and improved pointer question

    In my previous pointer post, I have a user input:
    Hello to the world
    and I converted this string to:
    olleH ot eht dlrow
    using this code:

    Code:
    istringstream break_apart(input);
    input = "";
    while (break_apart >> word)
      {
        front = &word.at(0);     
        rear = &word.at(word.size() - 1);
        while (front <= rear)     {
              swap (*front, *rear);
              front++;
              rear--;
        }
        input += word + " ";

    Using this sort of method of pointers, how would I change this code to have my pointers point to an ENTIRE word, not just a character like I have above? Thank you very much for all of the help in advance!!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You mean you want a pointer to the string instead of a pointer to an individual char?
    Code:
    string word = "word";
    string* word_pointer = &word;
    I'm not sure why you would want to do this though.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    I mean if I had the input "Hello to the world," I want to know how to assign a pointer to a word, so that I can do essentially the same thing as before, but have the program output, "world the to Hello" (backwards)

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Instead of
    Code:
    input += word + " ";
    you can do
    Code:
    input = word + input; // Might need to add a space in here as well, but you get the idea

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Store the words into a suitable container (eg a std::vector<std::string>), and then access elements from that container in reverse order.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    can you show me an example using pointers please grumpy? I want to keep my program consistant if you do not mind. Thank you though for your idea, that would definately make sense.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'll leave that as an exercise. Simply use the logic of my suggestion, and work out how to implement it using an array and a pointer to elements of that array.

    Incidentally, you might want to look up the standard header named <algorithm> and, within that header, the std::reverse() algorithm.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Please don't start a new thread on a topic already being discussed.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    its a new question though, its not the same question as previously. i had my disclaimer at the top there explaining the previous post. I am not breaking rules.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    OK, i fixed that part of my program, kinda, but now I have a new problem for some reason: My program will not output what I would like for it to be. This is weird, because it seems as if it would be the least of my worries, haha. I dunno. Even if I try to output something within a for loop toward the end of int main(), it doesn't for some odd reason...

    If you all haven't been following this post, the object of the program is to input a string and then reverse the order of the words. Ex.) "Hello to the world!" --> "world! the to Hello"

    The part I'm having trouble with (in bold) starts at the end of the sentence looking for spaces. When it finds a space, it copies each character to the output string between
    the space and the end of the sentence, then updates to the new
    location and looks for the next space.

    Maybe someone can see what I'm not. Please make my program output something!!! Thanks for the help!!


    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>  
    void swap (char *front, char *rear);
    using namespace std;
     
    int main()
    {
      string input, word;
      char *front, *rear;
      //vector <string> array; 
      cout << "Enter a string to reverse: ";
      getline(cin, input);
      
      typedef char* ArrayPtr; 
      ArrayPtr array, mod_array;
      array = new char[input.length()];
      string mod_input = input;
      mod_array = new char[mod_input.length()];
      istringstream break_apart(input); 
      input = ""; 
      
      while (break_apart >> word)
      {
        front = &word.at(0); 
        rear = &word.at(word.size() - 1); 
        while (front <= rear) 
        {
              swap (*front, *rear);
              front++;
              rear--;
        }
        input += word + " ";
      }
      
      for (int i = 0; i < input.length(); i++)
            array[i] = input[i];
      
      cout << "The formatted, reversed string is: ";
      for (int i = 0; i < input.length(); i++)
          cout << array[i];
      cout << endl;
      
      char* output;
      output = new char[mod_input.length()];
      int next_out_char = 0;
    
      int next_space_index = mod_input.length() - 1;
      int word_end_index = mod_input.length() - 1;
    
      while (next_space_index > -1)
      {
         while (mod_input[next_space_index] != ' ' && next_space_index >= -1)
             next_space_index--;
      }
      for (int j = next_space_index + 1; j < word_end_index; j++)
      {
          output[next_out_char] = mod_input[j];
          next_out_char++;
      }
      next_space_index--;
      word_end_index = next_space_index;
      return 0;
    }
    void swap (char *front, char *rear) 
    {
         char temp = *front;
         *front = *rear;
         *rear = temp;
    }

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is really confusing to understand what is going on here.

    When you use the stringstream to break the sentence up into words, and you can concatenate it all back into a string with the words in reverse order, what's all the rest for?

    -----

    Aside:

    Code:
    char *front, *rear;
    ...
    swap (*front, *rear);
    ...
    void swap (char *front, char *rear)
    {
         char temp = *front;
         *front = *rear;
         *rear = temp;
    }
    Did you actually know that you are using the standard algorithm swap instead of your swap, since the call with two characters (not pointers) doesn't match your swap? (The std::swap works fine, here, though.)
    Last edited by anon; 04-25-2009 at 03:59 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed