StackPointer
StackPointer

Reputation: 529

Reverse String C++ using char array

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?

Upvotes: 14

Views: 118265

Answers (6)

Maksiks
Maksiks

Reputation: 17

You can use strrev from the <cstring> header

strrev(str);

as simple as that. It accepts char arrays as well as strings.

Upvotes: 0

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char str[80];
    cout << "Enter a string bro: \n";
    gets_s(str);

    for (int i = strlen(str) - 1; i > -1; i--)
    {
        cout << str[i];
    }
}

Upvotes: -2

Cjolsen06
Cjolsen06

Reputation: 99

If I were you, I would just write it like so:

int main()
{
    string str;
    cout << "Enter a string: " << endl;
    getline(cin, str);
    for (int x = str.length() - 1; x > -1; x--)
    {
        cout << str[x];
    }
    return 0;
}

This is a very simple way to do it and works great.

Upvotes: -2

Kartik
Kartik

Reputation: 7

//reverse a string
#include<iostream>
using namespace std;

int strlen(char * str) {
  int len = 0; 
  while (*str != '\0') {
    len++;
    str++;
  }
  return len;
}

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

int main() {
  char str[100];
  cin.getline(str,100);
  reverse(str, strlen(str));
  cout<<str<<endl;
  getchar();
  return 0;
}

Upvotes: -1

Bill Lynch
Bill Lynch

Reputation: 81966

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.

Upvotes: 49

leemes
leemes

Reputation: 45685

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.

Upvotes: 1

Related Questions