Q Wolf
Q Wolf

Reputation: 43

what am i doing wrong?reverse string c++

so I want to do the simplest thing in c++ , reverse a string (store the new string) and than print it

my code is :

char a[size] , reverse[size];
strcpy(a,"dlow olleh " );
for (int i = 0 ;  i <= strlen(a); i++) {
   reverse[i]= a[strlen(a)-i];
}
cout << reverse ;

I must note that when cout << reverse[i] ; is inside the for loop every thing wotks fine , but when I wwant to print it as a string it just don't , I cant under stand what ive missed cout << reverse[i] ;

Upvotes: 0

Views: 487

Answers (3)

DarkImage
DarkImage

Reputation: 63

The first you copy when reversing the string is in fact the null terminator so when you are printing it to console it will not show up since the null terminator is the first in the array so you want to do this instead

int size = 12;
char a[12], reverse[12];
strcpy(a, "dlow olleh ");
for (int i = 0; i < strlen(a); i++) {
    reverse[i] = a[strlen(a) - (i+1)];
}
reverse[strlen(a)] = '\0';
cout << reverse;

Upvotes: -1

J. Doe
J. Doe

Reputation: 496

what am i doing wrong?

You are using arrays of char and functions of the C Standard Library to manipulate strings in C++.

#include <string>
#include <algorithm>
#include <iostream>

int main()
{
    std::string foo{ "Hello, World!" };
    std::string bar{ foo };
    std::reverse(bar.begin(), bar.end());
    std::cout << '\"' << foo << "\" ==> \"" << bar << "\"\n";
}

If – for some reason beyond my comprehension – you *have to* do it by foot, do it in an idiomatic way and provide an interface that takes a pair of iterators:

#include <algorithm>

void str_reverse(char *begin, char *end)
{
    while (begin < end)
        std::swap(*begin++, *--end);
}

// ...

#include <cstring>
// ...
char foo[]{ "Hello, World!" };
str_reverse(foo, foo + std::strlen(foo));

If you can't use <algorithm> for whatever reason implement your own swap():

template<typename T>
void swap(T &a, T &b)
{
    T tmp{ a };
    a = b;
    b = tmp;
}

Upvotes: 6

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

In this loop

for (int i = 0 ;  i <= strlen(a); i++){
       reverse[i]= a[strlen(a)-i];

you are accessing characters beyond the actual characters of the strings.

For example when i is equal to 0 you are coping the terminating zero character from the string a into the first position of the string reverse.

reverse[0]= a[strlen(a)-0];

the code can be written simpler without for example reduntant calls of the function strlen.

char a[size], reverse[size];
strcpy( a, "dlrow olleh" );

size_t i = 0;
for ( size_t n = strlen( a ); i < n; i++ ) 
{
    reverse[i] = a[n - i - 1];
}
reverse[i] = '\0';

std::cout << reverse << '\n';

Pay attention to that there is the standard algorithm std::reverse_copy that does the same task.

Below there is a demonstrative program.

#include <iostream>
#include <algorithm>
#include <cstring>

int main() 
{
    const size_t SIZE = 20;
    char a[SIZE], reverse[SIZE];

    std::strcpy( a, "dlrow olleh" );    

    std::cout << a <<'\n';

    auto it = std::reverse_copy( a, a + strlen( a ), reverse );
    *it = '\0';

    std::cout << reverse <<'\n';

    return 0;
}

The program output is

dlrow olleh
hello world

Upvotes: 5

Related Questions