Reverse By Word In Dynamic character Array

Hey guy, this is the first time i am going to post here, so let me get straight to the problem

I WROTE THIS :
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
void ReverseByWords(char *Arr, char *&RArr)
{
	int j = 0, k = 0;
	RArr = new char[strlen(Arr) + 1];
	for (int i = strlen(Arr); i >= 0; i--)
	{
		if (*(Arr + i) == ' ')
		{
			*(Arr + i) = '\0';
			i++;
			k = i;
			do
			{
				*(RArr + j) = *(Arr + k);
				j++;
				k++;
			} while (!(*(Arr + k) == '\0'));
			*(RArr + j) = ' ';
			j++;
		}
	}
	k = 0;
	do
	{
		*(RArr + j) = *(Arr + k);
		j++;
		k++;
	} while (!(*(Arr + k) == '\0'));
}



So... what it does is THIS :
input (To "Arr" From File) : I LOVE PROGRAMMING
output :                     PROGRAMMING LOVE I


it does this but i think its the worst way to do so, so basically im looking for a better idea to implement this.
Help Appreciated,
Thanks.
Last edited on
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
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;


void reversePart( char word[], int first, int last )
{
   while ( first < last ) swap( word[first++], word[last--] );
}


bool findWordLimits( char word[], int &p, int &q, int last )     // Find next word limits, starting at p
{                                                                // Return via p and q and return true iff found word
   while ( p <= last && word[p] == ' ' ) p++;
   if ( p > last ) return false;
   q = p + 1;
   while ( q <= last && word[q] != ' ' ) q++;
   q--;
   return true;
}


void reverseSentence( char *In, char *Out )
{
   strcpy( Out, In );                            // Copy In into Out as a work array
   int last = strlen( Out ) - 1;
   int p = 0, q;
   while ( findWordLimits( Out, p, q, last ) )   // Find next word
   {
      reversePart( Out, p, q );                  // Reverse that word
      p = q + 1;
   }
   reversePart( Out, 0, last );                  // Now reverse the lot
}                                                // Out should already be null-terminated from strcpy


int main()
{
   char A[] = "this is hopeless";
   char *B = new char[strlen(A)+1];              // Set size in calling program; it's easier
   reverseSentence( A, B );
   cout << A << '\n' << B << '\n';
   delete [] B;
}

Last edited on
It' a reasonable algorithm as long as you don't mind filling the input array with nuls. I suppose you could restore them to spaces before returning. But you don't really need to write the nuls in the first place. And it's better to return the output array instead of passing it back in a reference parameters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char *ReverseByWords(const char *in) {
    size_t len = strlen(in), i_out = 0, i_end = len;
    char *out = new char[len + 1];

    for (size_t i_in = len; i_in-- > 0; )
        if (in[i_in] == ' ') {
            for (size_t i = i_in + 1; i < i_end; )
                out[i_out++] = in[i++];
            out[i_out++] = ' ';
            i_end = i_in;
        }

    for (size_t i = 0; i < i_end; )
        out[i_out++] = in[i++];

    out[i_out] = '\0';
    return out;
}


You could also do it with pointer notation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char *ReverseByWords(const char *in) {
    size_t len = strlen(in);
    char *out = new char[len + 1], *pout = out;
    const char *pend = in + len;

    for (const char *pin = pend; pin-- > in; )
        if (*pin == ' ') {
            for (const char *p = pin + 1; p < pend; )
                *pout++ = *p++;
            *pout++ = ' ';
            pend = pin;
        }

    for (const char *p = in; p < pend; )
        *pout++ = *p++;

    *pout = '\0';
    return out;
}

Last edited on
Topic archived. No new replies allowed.