Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
Reversing a string in C

I'm currently switching from C++ to C programming for a project and I haven't done much with Char arrays as strings. I need a function that will read in a pointer to a char array and reverse it. I wrote this in C++, which is pretty easy using the string functions, but I'm a little confused on if there are functions or something else in C that is the best way to do this. Thanks, and I'm not necessarily looking for someone to completely finish the code, but to point me in the right direction. If it's simple one line something feel free, but don't do anything that makes you feel uncomfortable.

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

void reverseString(char *myString)
{
    //reverse string here
}

int main(void)
{
    char myString[] = "This is my string!";
    reverseString(myString);

    return 0;
}
share|improve this question

marked as duplicate by WhozCraig, Musa, H2CO3, Blastfurnace, Vlad Lazarenko Dec 10 '12 at 5:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 2 down vote accepted

Simplest way: Loop the string char by char and insert each char to another char array in the reverse order.

Or try this: 2)

void reverse_string(char str[])
{
    char c;
    char *p, *q;

    p = str;
    if (!p)
        return;

    q = p + 1;
    if (*q == '\0')
        return;

    c = *p;
    reverse_string(q);

    while (*q != '\0') {
        *p = *q;
        p++;
        q++;
    }
    *p = c;

    return;
}

3)

if( strlen( str ) > 0 ) {
   char* first = &str[ 0 ];
   char* last = &str[ strlen( str ) - 1 ];
   while( first < last ) {
       char tmp = *first;
       *first = *last;
       *last = tmp;
      ++first;
      --last;

4)

char* strrev( char* s )
  {
  char  c;
  char* s0 = s - 1;
  char* s1 = s;

  /* Find the end of the string */
  while (*s1) ++s1;

  /* Reverse it */
  while (s1-- > ++s0)
    {
    c   = *s0;
    *s0 = *s1;
    *s1 =  c;
    }

  return s;
  }
share|improve this answer
1  
Why the downvote? –  irrelephant Dec 10 '12 at 5:50
1  
This is by no means the best way to do this. Plus IMHO the asker should be redirected to exact duplicates. -1 –  axiom Dec 10 '12 at 5:52
2  
@axiom "should be redirected" is not a good enough reason for downvoting a valid answer, neither is 'this is not the best way' for doing so. If you always expect everybody to do everything in the best way, you can do that, but only if you're a perfect programmer (something I seriously doubt). –  user529758 Dec 10 '12 at 5:54
    
@H2CO3 I am no way even near perfection sir, so your doubts are justified. Sorry if i ever implied that i am perfect. Just that i happen to know a bit about what's being discussed. Neither did i down vote just because it is an exact duplicate. I just didn't like the one sentence solution presented in plain English earlier ( take another array and just copy in reverse) –  axiom Dec 10 '12 at 5:57
1  
@axiom Oh, I see. Sorry for disturbin in this case. –  user529758 Dec 10 '12 at 5:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.