Reverse string array

Don't know how to realize a function or a code which reverse string array. Reversing string array is different from reversing number arrays.
Reversing string array is different from reversing number arrays.
Think of a string as an array of characters, then you'll see the algorithm is the same.
Not a string, I need to reverse string array.
Last edited on
It is still the same. Logic is exactly the same. Array of std::string objects? Swap strings around. Array of c-strings pointers? Swap pointers around.
Can my code do 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
30
31
32
33
34
while(c>=b)
{
d[counter]=c%b;
c=c/b;
counter++;
}
d[counter++]=c;
LabeledEdit3->Text="";
if(counter%2==0)
{
for(i=0;i<counter/2;i++)
{
temp=d[counter-1-i];
d[counter-1-i]=d[i];
d[i]=temp;
}
for(i=0;i<counter;i++)
{
LabeledEdit3->Text=LabeledEdit3->Text+FloatToStr (d[i]);
}
}
else
{
for(i=0;i<(counter-1)/2;i++)
{
temp=d[counter-1-i];
d[counter-1-i]=d[i];
d[i]=temp;
}
for(i=0;i<counter;i++)
{
LabeledEdit3->Text=LabeledEdit3->Text+FloatToStr (d[i]);
}
}
It is unnesesary complicated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//typedef const char* string;
//typedef std::string string;

void swap(string& l, string& r)
{
    string temp = l;
    l = r;
    r = temp;
}

void reverse(string arr[], size_t n) 
{
    string* end = arr + n;
    while(arr < --end)
        swap(*(arr++), *end)      
}


Example of output: I inputed "10 9 8 7 6 5 4 3 2 1 0", and output will be:"0 1 2 3 4 5 6 7 8 9 10", not "0 1 2 3 4 5 6 7 8 9 01"
Last edited on
Yes. If you properly place your input in array of strings, then my reverse function will work fine.
In your second example you have shown reversing a single string, not an array of stings.

Reversing all kind of arrays follows the exact same logic.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>

int main()
{
    char str1[] = "0string1inReverse00";
    size_t length = strlen(str1);
    
    char *str2 = new char[length];
    str2[length] = '\0';
    
    char *ptr1 = &str1[0];
    char *ptr2 = &str2[length - 1];
    
    while ( *ptr1 != '\0' ) {
        *ptr2 = *ptr1;
        ptr1++;
        ptr2--;
    }
    
    std::cout << str2 << std::endl;
    
    return 0;
}

Oops: I missed the comment before last. see Borges solution
Last edited on
MiiNiPaa, my BCB (Borland C++ Builder) said that 'string' can't start a parameter declaration and he expected type name instead of void.
Last edited on
string in my code is a placeholder, because you have never answerde what string type do you use.

I provided two commented out type aliaces which you can uncomment to make function work with corresponding type.

http://coliru.stacked-crooked.com/a/3dd8cbff3865eda3
> I inputed "10 9 8 7 6 5 4 3 2 1 0", and output will be:"0 1 2 3 4 5 6 7 8 9 10", not "0 1 2 3 4 5 6 7 8 9 01"

I assume that means: reverse the order of white-space separated tokens in a string, but retain the original order of characters within each token?

C++14, so Borland C++ Builder will choke on this.
But if it has something resembling std::string, this simple, brute-force, approach can still be used:
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
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
#include <iomanip>

std::string reverse_token_order( const std::string& str )
{
    std::string reversed ;

    std::string token ;
    for( char c : str )
    {
        if( std::isspace(c) )
        {
            reversed = c + token + reversed ;
            token.clear() ;
        }
        else token += c ;
    }
    reversed = token + reversed ;

    return reversed ;
}

char* reverse_token_order( char* cstr ) // invariant: not null
{
    const std::string reversed = reverse_token_order( std::string(cstr) ) ;
    return std::strcpy( cstr, reversed.c_str() ) ;
}

int main()
{
    char cstr[] = "10 9     8    7   6  5 4 3 2 1 0" ;
    reverse_token_order(cstr) ;

    const std::string str = "12 345     6789 hello world!    " ;

    std::cout << std::quoted(cstr) << "\n\n"
              << std::quoted( reverse_token_order(str) ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/edbbcb55747810e9
Topic archived. No new replies allowed.