Pointer Question

Pages: 12
Method 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
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);
  }
   return 0;
}

Method 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string input, word;
  char front[1], rear[1];
  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[0] = word.at(0);
    cout << front[0];
    rear[0] = word.at(word.size() - 1); 
    cout << rear[0];
    string.push_back(word);
  }
Last edited on
great that helped out a ton! ok, so i've been working on it a little bit more, and I'm having trouble trying to find a way to output the reversed string. Outputting the first and last character, and then working my way in is easy, but if I enter:
Programming
I want to see
gnimmargorP
not the way I have it. Does anyone know how to do this? Thanks for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (break_apart >> word)
  {
       for (int i = 0; i < (word.size() / 2); i++)
       {
           front = &word.at(i);
           rear = &word.at(word.size() - 1 - i);
           
           char temp = *front;
           *front = *rear;
           *rear = temp;
           cout << *front << " " << *rear << " ~ ~ ";
       }
        
    string.push_back(word);
  }
Last edited on
there you go kevinchkin
Topic archived. No new replies allowed.
Pages: 12