Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse String C++ using char array

Tags:

c++

arrays

string

I wrote a simple C++ program to reverse a string. I store a string in character array. To reverse a string I am using same character array and temp variable to swap the characters of an array.

#include<iostream>
#include<string>
using namespace std;

void reverseChar(char* str);

char str[50],rstr[50];
int i,n;

int main()
{
    cout<<"Please Enter the String: ";
    cin.getline(str,50);
    reverseChar(str);
    cout<<str;
    return 0;
}

void reverseChar(char* str)
{
    for(i=0;i<sizeof(str)/2;i++)
    {
        char temp=str[i];
        str[i]=str[sizeof(str)-i-1];
        str[sizeof(str)-i-1]=temp;
    }
}

Now this method is not working and, I am getting the NULL String as result after the program execution.

So I want to know why I can't equate character array, why wouldn't this program work. And what is the solution or trick that I can use to make the same program work?

like image 646
StackPointer Avatar asked Jun 23 '14 18:06

StackPointer


People also ask

How will you reverse the string in more than 2 ways in C?

A given string can be reversed in the C language by using strrev function,without strrev, recursion, pointers, using another string, or displaying it in reverse order.


2 Answers

The problem is that within your function, str is not an array but a pointer. So sizeof will get you the size of the pointer, not the length of the array it points to. Also, even if it gave you the size of the array, that is not the length of the string. For this, better use strlen.

To avoid multiple calls to strlen, give the function another parameter, which tells the length:

void reverseChar(char* str, int len)
{
    for(i=0; i<len/2; i++)
    {
        char temp=str[i];
        str[i]=str[len-i-1];
        str[len-i-1]=temp;
    }
}

and call it with

reverseChar(str, strlen(str))

Another improvement, as mentioned in the comments, is to use std::swap in the loop body:

void reverseChar(char* str, int len)
{
    for(i=0; i<len/2; i++)
    {
        std::swap(str[i], str[len-i-1]);
    }
}

Also, there is std::reverse which does almost exactly that.

like image 37
leemes Avatar answered Oct 14 '22 14:10

leemes


sizeof(str) does not do what you expect.

Given a char *str, sizeof(str) will not give you the length of that string. Instead, it will give you the number of bytes that a pointer occupies. You are probably looking for strlen() instead.

If we fixed that, we would have:

for(i=0;i<strlen(str)/2;i++)
{
    char temp=str[i];
    str[i]=str[strlen(str)-i-1];
    str[strlen(str)-i-1]=temp;
}

This is C++, use std::swap()

In C++, if you want to swap the contents of two variables, use std::swap instead of the temporary variable.

So instead of:

char temp=str[i];
str[i]=str[strlen(str)-i-1];
str[strlen(str)-i-1]=temp;

You would just write:

swap(str[i], str[sizeof(str) - i - 1]);

Note how much clearer that is.

You're using C++, just use std::reverse()

std::reverse(str, str + strlen(str));

Global variables

It's extremely poor practice to make variables global if they don't need to be. In particular, I'm referring to i about this.

Executive Summary

If I was to write this function, it would look like one of the two following implementations:

void reverseChar(char* str) {
    const size_t len = strlen(str);

    for(size_t i=0; i<len/2; i++)
        swap(str[i], str[len-i-1]);
}

void reverseChar(char* str) {
    std::reverse(str, str + strlen(str));
}

When tested, both of these produce dlrow olleh on an input of hello world.

like image 119
Bill Lynch Avatar answered Oct 14 '22 16:10

Bill Lynch