Pointer Question

Pages: 12
Apr 17, 2009 at 3:52am
I have a question concerning pointers. If the program user inputs the phrase:

Cplusplus is awesome


How do I make my program output:

sulpsulpC si emosewa
(make every word read out backwards)

Please help me out ASAP. Thanks for the help in advance!
Apr 17, 2009 at 4:27am
Take a look at this:
http://cplusplus.com/reference/algorithm/reverse/

Now you could break the sentence up into the separate words or you could use the space as the delimiter. Reverse can work for you even if you keep the entire sentence in one string. std::string provides substr and find functions to help you with that. Use find to find the iterator pointing to the space. You can then have a start and stop iterator for each word that can be used with std::reverse.
Apr 17, 2009 at 6:15am
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
#include <iostream>
#include <algorithm>
#include <string>


int main()
{
    // create a string
    std::string strObject("Hello to the whole world ");

    // initialize iterator and offset
    std::string::iterator start = strObject.begin();
    std::string::iterator space = strObject.begin();
    std::string::iterator end = strObject.end();

    // reverse each word
    while(end != (space = std::find(start, end, ' ')))
    {
        // after the call, increment the iterator and
        // copy it to the new start
        std::reverse(start, space);
        start = ++space; // move past the space
    } // end while.  ends when no more spaces are found

    // reverse the last word unless the sentence ended with a ' '
    if(start != strObject.end())
    {
        std::reverse(start, end);
    }

    // print the result
    std::cout << strObject << std::endl;


    return 0;
}


I had an itch to write some code so I just did part of it. You still have to figure out the user input. I just realized that I hate std::string::find. I don't understand why they coded it to return a position as an integer rather than an iterator. I sure wish that they would have overloaded the find but maybe it's because the std::find already does that.
Apr 17, 2009 at 6:20am
Thanks for the help!
Apr 17, 2009 at 12:56pm
Well, basic_string came before the whole iterator paradigm, IIRC.
Apr 20, 2009 at 1:09am
ok, so im not gonna use reverse, cuz that's kinda cheap. so what i want to do, (use my above example) is kinda like a palindrome tester. You take one word, you assign the first character to a pointer variable and you assign the last character of that SAME word a different character, and then you work inwards. can any one get me started please? Sorry kempofighter, I don't really understand your example that much, but I appreciate the ideas.
Last edited on Apr 20, 2009 at 1:11am
Apr 20, 2009 at 1:24am
Strings have iteraterors that start at begin() and end at end(), and reverse_iterators that start at rbegin() and end at rend().

Apr 20, 2009 at 1:28am
but i don't want to use those, i would like to use pointer variables to do this, i do not want to use member functions.
Apr 20, 2009 at 1:31am
Iterators are C++'s functional equivalent to pointers.
Apr 20, 2009 at 1:46am
is it possible to get me going on the right track the way i described? i appreciate the help
Apr 20, 2009 at 2:31am
You are on the right track with your palindrome idea. std::reverse() used std::swap() to switch the first and last characters, working inward until the iterators (pointers) meet.
Apr 20, 2009 at 4:46am
can i have an example of what you mean. I have never used those before, and it sounds intriguing.
Apr 21, 2009 at 12:12am
Something like this...

1
2
3
4
5
6
7
8
9
10
    char word[] = "Cplusplus";
    char* start = word;
    char* end = start + strlen(word) - 1;
    while (start < end)
    {
        std::swap(*start, *end);
        ++start;
        --end;
    }
    std::cout << word << std::endl;


Apr 21, 2009 at 2:57am
OK, im not on the swapping part yet, but I have a problem with seperating the words. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <vector>  
 
using namespace std;
 
int main()
{
  string input, word; 
  vector <string> string;  
  cout << "Enter a sentence that you would like to reverse: ";
  getline(cin, input);
 
  while (input >> word)
  {
    cout << word << endl;
    string.push_back(word);
  }
  return 0;
}


...and I'm getting an error that reads:

no match for 'operator>>' in 'input >> word'


why is this, and what should it look like. Thanks!
Apr 21, 2009 at 3:20am
Excellent! Very good idea you have there for splitting words.

Here's a hint: http://www.cplusplus.com/reference/iostream/istringstream/
Last edited on Apr 21, 2009 at 3:28am
Apr 21, 2009 at 5:06am
is there an alternative to this, or is this the only way?!
Apr 21, 2009 at 5:19am
I'm sure there are many ways of doing this. It sounds like you are asking us, "what is the most primitive and overly complex way to do something that has already been done more simply?" Perhaps you should quit using c++ libraries completely and just use char* and c run time libraries. I'm sure that you can follow my original concept without iterators. You can use operator[] with character arrays. If you fiddle with the for loops you can do it that way character by character. Study the example provided by the std::reverse example on this website and you can do the same thing with operator[].
Apr 21, 2009 at 5:26am
When I run my program in which this chunk is embedded in, the program doesn't output anything and it just closes. why is that?!

1
2
3
4
5
6
7
8
while (break_apart >> word)
  {
    *front = word.at(0);
    cout << *front;
    *rear = word.at(word.size() - 1);
    cout << *rear;
    string.push_back(word);
  }


P.S. I'm new to pointers, so be nice. >.<
Apr 21, 2009 at 6:10am
By closing do you mean error closing? If yes, then may be there is no word either at word.at(0) or word.at(word.size() - 1); and may be it is accessing some garbage address. So when you try to dereference it, it breaks. Depending on at which cout it is breaking. You can try to debug it using gdb and check what exactly are the contents of word.at(0) and word.at(word.size() - 1)
But if it is a normal termination then word.at(0) and word.at(word.size() - 1); are either NULL or may be space character.

Last edited on Apr 21, 2009 at 6:12am
Apr 21, 2009 at 6:15am
well, here is the code that i have so far, excluding all of the given stuff. it all works and compiles as expected, until this point where it just closes the running problem and claims there is an error. would this be a run-time error?! i don't know. maybe you might know what is wrong... thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string input, word;
  char *front, *rear; 
  vector <string> string; // This will hold the individual words temporarily until they are replaced
 
  cout << "Enter a sentence that you would like to reverse: ";
  getline(cin, input);
 
  // Puts the string into a stream
  istringstream break_apart(input);
 
  while (break_apart >> word)
  {
    *front = word.at(0);
    cout << *front;
    *rear = word.at(word.size() - 1);
    cout << *rear;
    string.push_back(word);
  }
Pages: 12