Jump to: navigation, search

I wrote a different solution to this, because I assumed (before looking at the answers), that I couldn't use a separate recursive function, but had to make the main void reverse(char *s) function recursive. It is below. If it is accepted, feel free to move it onto the page. Comments/pedantic corrections gratefully accepted. -- JesseW 20:05, 6 July 2007 (BST)

Updated per Netocrat's suggestions. JesseW 20:38, 10 July 2007 (BST)
/* Recursive string reverse function, without using any setup-wrapper function. 
   Released by Jesse Weinstein, Tue Jul 10 12:37:50 2007 */

void shift(char *s);
void reverse(char *s);

void reverse(char *s) 
{
  int len;
  for (len = -1; s[++len] != '\0'; )
    ;
  if (s[len-1] == '\n')
    len--;
#ifdef DEBUG
  printf("len:%i, s:%s\n", len, s);
#endif
  if (len > 0) {
    reverse(&s[1]);
  }
  if (len > 1) {
    shift(s);
  }
#ifdef DEBUG
  printf("<= len:%i, s:%s\n", len, s);
#endif
}

void shift(char *s) 
{
  int i;
  char tmp;
  tmp = s[0];
  for (i = 0; s[i+1] != '\0'; i++) {
    s[i] = s[i+1];
  }
  s[i] = tmp; /* i is set to the character before the \0; 
		 the last time through the loop sets s[i] to '\0' */
}

int main(void) 
{
  char in[] = "Hello, world!";
  printf("Input: \"%s\"\n", in);
  reverse(in);
  printf("Reversed: \"%s\"\n", in);
  reverse(in);
  printf("Twice reversed: \"%s\"\n", in);
  return 0;
}

Neat solution, Jesse. I have one pedantic comment to make re style - in particular whitespace: you seem to use it inconsistently - your code would be more readable if you stuck with a guideline. Personally I like to use a single space before and after a binary operator, a single space following the semicolons in for loop declarations, and to not separate the pointer operator (*) from its associated variable name. Also, if you want to be compatible with C89 then it's useful to avoid // style comments and to stick with /* */ style.

Oh, one other minor nitpick - I would have had the "shift" function perform the task of moving the first character to the end rather than performing that task around and outside of that function.

To me though your code looks suitable for the page - why don't you add it there? --Netocrat 08:45, 10 July 2007 (BST)

OK, I've taken your suggestions, and put it on the page. Thanks! JesseW 20:38, 10 July 2007 (BST)

Hello and welcome everybody. I would like to suggest this sollution:

void reverse(char s[])
{
     static int i = 0;
     static int j;
     static int k;
     static int temp;
     
    if (i == 0) k=((j=strlen(s)-1)/2);
    if (i < k) {
          i++;
          j--;
          reverse(s);
    }
    
    temp = s[j];
    s[j] = s[i];
    s[i] = temp;
    i--;
    j++;
    
    if (i < 0) i = 0;      
}

This version doesn't use pointers, and also the user only has to input the string o characters with no extra parameters. However, I do not know how this function would work when an interrupt comes. I would like to hear your comments on that. WielkiZielonyMelon 17:55, 27 February 2009 (UTC)


This is what I ended up with, it's similar to the above but without string.h

void reverse(char s[], int i, int j) {
        if(i == 0 && j == 0)
                j = len(s) - 1;
        if(i < j) {
                swap(s, i++, j--);
                reverse(s, i, j);
        }
}

int len(char s[]) {
        int i = 0;
        for(i = 0; s[i] != '\0'; i++);
        return i;
}

void swap(char s[], int i, int j) {
        char tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
}

--Blentz (talk) 04:57, 24 January 2016 (UTC)

Personal tools