Reverse a string

Why is the reverse function giving me error while running it?
It is right at the assignment *p = *len statement.

void reverse(char *str) {
      char *len = &str[strlen(str)-1];
      char tmp;
      for (char* p = str; p < len; p++, len--) {
            tmp = *p;
            *p = *len;
            *len = tmp;
      }
}

int main(int argc, char* argv[])
{
      reverse("barbeque");
      return 0;
}
chunichAsked:
Who is Participating?
 
bachra04Commented:
You access a non permitted memory, you should allocate enough space to do the reverse using static or dynamic memory allocation.

please try the following code, it should work:

#include <stdio.h>
#include <string.h>

void reverse(char *str) {
     char *len = &str[strlen(str)-1];
     char tmp;

     for (char* p = str; p < len; p++, len--)
       {
          tmp = *p;
          *p = *len;
          *len = tmp;
     }
}

int main(int argc, char* argv[])
{
       char szTemp[10]= "barbeque";
       reverse(szTemp);
       printf ("%s\n", szTemp);
     return 0;
}
0
 
brettmjohnsonCommented:
The string "barbeque" is a string constant.  Many compilers store string constants in read-only memory, so when you try to re-arrange the characters you get a memory access violation.  Rather than trying to modify a string constant, modify a mutable string that has been initialized to that constant value.

int main(int argc, char* argv[])
{
     char str[] = "barbeque";
     reverse(str);
     return 0;
}
0
 
jhanceCommented:
By the way, while it's not a part of the std. "C" RTL, many implementation (in fact MOST) have an implementation of a string reversal function.  In MS Visual C it's _strrev().  Most others use strrev() or similar.  In my opinion it's best NOT to re-invent the wheel if you can help it.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
brettmjohnsonCommented:
jhance,

The "Reverse a string" problem is a very common academic assignment, used to teach the use of pointers, arrays, and C strings.  The students are discouraged from using the library routine to accomplish the task, since they really would not learn anything.
0
 
jhanceCommented:
They also don't learn much by getting their homework done by experts here.  But that's another story...
0
 
bachra04Commented:
//They also don't learn much by getting their homework done by experts here.  But that's another story...

This is not the case in this thread, the algorithm proposed by the student is correct but he has only a problem of memory allocation and need help just at this point  so 90% of the job is done by him self and not by experts and I think that's correct .

B.T
0
 
chunichAuthor Commented:
Thank you guys so much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.