?
Solved

Reverse a string

Posted on 2006-04-07
7
Medium Priority
?
470 Views
Last Modified: 2008-02-01
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;
}
0
Comment
Question by:chunich
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 200 total points
ID: 16405510
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
 
LVL 2

Accepted Solution

by:
bachra04 earned 200 total points
ID: 16405650
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
 
LVL 32

Expert Comment

by:jhance
ID: 16407616
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 16408629
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
 
LVL 32

Expert Comment

by:jhance
ID: 16409028
They also don't learn much by getting their homework done by experts here.  But that's another story...
0
 
LVL 2

Expert Comment

by:bachra04
ID: 16411827
//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
 

Author Comment

by:chunich
ID: 16417833
Thank you guys so much.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Loops Section Overview

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question