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;
}
|