Ullan
Ullan

Reputation: 1007

How to Reverse a string using C?

I am trying to run the below code in Visual Studio 2008. But at run time the program is throwing an error

Unhandled exception at 0x002e1480 in reverseString.exe: 0xC0000005: Access violation writing location 0x002e573c.

void reverse(char *str)
{
    char *end = str;
    char tmp;
    if (str)
    {
      while (*end)
      {
         ++end;
      }
      --end;

      while (str < end) 
      {
        tmp = *str;
        *str++ = *end; // Error Here
        *end-- = tmp;
      }
   }
}

Thanks in advance for your great help.

Upvotes: 1

Views: 504

Answers (6)

P0W
P0W

Reputation: 47784

Another approach, if you don't like/want iterator or reverse function.

 string revStr(string str){
        if (str.length() <= 1) {
            return str;
        }else{
            return revStr(str.substr(1,str.length()-1)) + str.at(0);
        }
    }

Upvotes: 0

Christian Wilkie
Christian Wilkie

Reputation: 3823

I think you are calling it like so

char* mystr = "Hello";
reverse(mystr);

right? But you can't do that since reverse is modifying the string (see here) which leads to the access violation. Call it as others have suggested by using

char mystr[] = "Hello";
reverse(mystr)

That being said using std::reverse as suggested by the others is a better solution.

Upvotes: 0

Caesar
Caesar

Reputation: 9841

Here is how to do it from scratch

#include <iostream>

void reverse(char* input)
{
    int size = strlen(input);
    int half = size / 2;

    for(int i = 0; i < half; ++i)
    {
        char temp = input[size - i - 1];
        input[size - i - 1] = input[i];
        input[i] = temp;
    }
}

int main()
{
    char word[] = "hello";
    reverse(word);
    std::cout << word;
}

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153820

It's trivial to reverse a string:

if (str) {
    std::reverse(str, str + strlen(str));
}

However, the precondition for this reversal to work is that str points to a writable string, i.e., it is declared as char* and is not initialized from a string literal! Although string literals can be used initialize char* objects, they do not necessarily point to writable memory (and on many contemporary system they do not point to writable memory).

In case you wonder how a naive implementation of std::reverse() could look like:

template <typename BiDirIt>
void reverse(BiDirIt begin, BiDirIt end) {
    for (; begin != end && begin != --end; ++begin) {
         swap(*begin, *end);
    }
}

Upvotes: 2

jmstoker
jmstoker

Reputation: 3475

I was able to use your reverse method with no issues, see below. The problem must be in how you're using the char* that you're passing in.

int main()
{
    char str[] = "Hello World!";

    reverse(str);

    cout << str << endl;

    return 0;
}

Here's the output

!dlroW olleH

Upvotes: 0

user541686
user541686

Reputation: 210427

std::reverse(str, str + std::char_traits<char>::length(str));

Note that reversing a string will result in broken text for many languages and encodings.

Upvotes: 1

Related Questions