New 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
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!!
Last edited on
what exactly you want.. its not clear??
pointer can point to one address only... word is a combination of multiple addresses!!
You mean something like this?
1
2
3
4
5
char *pointers[]={
    "A",
    "whole",
    "word"
};

Each of those string literals evaluate to pointers to char arrays at compile time.
Last edited on
I would assume it would be something like that. @ writeonsharma, I just assumed you could achieve this goal by using pointers. I guess not, and what you said totally makes sense. So, if you can't use pointers to achieve this goal, what CAN i do to figure this out?! thanks!
It might help if you told us how you intend to transform the input. What does the input look like and what should the corresponding output look like?
No problem. If you had input "Hello to the world", I need a way of breaking up this string into an array word[](this is the best way I can think of it, there may be a better way), so that I can cout << word[0] would output "Hello", and so on and so forth. What I would like for the program to accomplish is when the user inputs "Hello to the world", I would like for the program to reverse the order of the words so that the output is "world the to Hello" or:

cout << word[3] << word[2] << word[1] << word[0]

Please let me know if that doesn't make sense. Thank you to all the help!!!
I've been messing with the portion of my code to accomplish this, and here is where I am. I realize that it's still outputting the same input, but I don't know how to reverse the order of the words

If someone can let me know what I can do to fix that, I would be greatly appreciative!!! Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
istringstream break_phrase_apart(mod_input);
  mod_input = "";
  int count = 0;
  while (break_phrase_apart >> word)
  {
     for (int k = count; k >= 0; k--)
     {
         mod_input += word + " ";
     }
  }
  
  for (int x = 0; x < mod_input.length(); x++)
      cout << mod_input[x];
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!!

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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;
}
Huh? You aren't trying to output anything. What do you mean? I see you trying to fill a character array with some data but where are you trying to output something?
closed account (48T7M4Gy)
use strtok() function

This program:
1. reads in line of text
2. breaks it down into tokens (words)
3. saves each word into an array - counts as it goes - until the end of the line
4. prints out all the words in forward order (as a check)
5. prints out the words in reverse order

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
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

const int MAX_WORDS = 20;
const int MAX_LINE_LENGTH = 100;

void main( )
{
	char separators[]   = " ,\t\n";
	char* token;
	char* word[ MAX_WORDS ];
	char line[ MAX_LINE_LENGTH ];

	cout << "Please enter a sentence: ";
	cin.getline( line, MAX_LINE_LENGTH, '\n' );

	token = strtok( line, separators );
	int n = 0;
	while( token != NULL )
	{
		word[n] = token;
		token = strtok( NULL, separators );
		n++;
	}

	for( int i = 0; i < n; i++ )
		cout << word[ i ] << endl;

	for( int i = n - 1; i >= 0; i-- )
		cout << word[ i ] << endl;
}
Last edited on
After executing the code with the string reverse the string, this while loop was stuck in a continuous loop. You should have been able to debug that yourself.

1
2
3
4
5
while (next_space_index > -1)
  {
     while (mod_input[next_space_index] != ' ' && next_space_index >= -1)
         next_space_index--;
  }


What do you suspect happens after the internal while loop ends and next_space_index has a value of 11? How will the outer loop ever end? Your program doesn't seem to be trying to reverse the words, you are simply reversing the characters within each word while leaving the words in their original places. As far as the last few loops in the program go, I can't figure out what you expect them to do. Go ahead and use strtok as mckenzie suggested provided that you are aware of the security problems associated with that solution. It is functional and it works. To be honest, this is much simpler than you seem to think. All you have to do is break the sentence into an array of std::string objects. Given the set of requirements you gave, it is really that simple. Then you can iterate forward and backwards through the string array printing each string forward and backwards. You don't really have to play games trying to reverse anything or manipulate any of the strings. Most of the stuff in this program is complete nonsense and totally unnecessary.
Topic archived. No new replies allowed.