Thread: strrev()

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    strrev()

    is this a good version of it I found it on the internet.
    Code:
    char *strrev(char *s){
                    int i,n;
                    char *reverse;
    
                    for(i=0;s[i]!='\0';i++)
                    ;
    
                    reverse=malloc(i*sizeof(char));
                    n=i-1;
                    for(i=n;i>=0;i--)
                                    reverse[n-i]=s[i];
                    reverse[n+1]='\0';
                    return reverse;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Only truly bad thing I see is no error checking on malloc.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Isn't it better that the caller assigns memory and passes a pointer in? This way you're less likely to forget to free it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is this a good version of it
    No, not really. It's obscure and buggy, and if at all possible you should keep memory management in the same function. That way it's easier to remember to free it.

    >reverse=malloc(i*sizeof(char));
    Whoever wrote this forgot about an extra spot for the '\0'. IMHO, a better way would be like so:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *jstrrev ( char *dst, char *src )
    {
      int s = 0, e;
    
      for ( e = 0; src[e] != '\0'; e++ )
        ;
      while ( e != 0 )
        dst[s++] = src[--e];
      dst[s] = '\0';
    
      return dst;
    }
    
    int main ( void )
    {
      char r[5];
    
      puts ( jstrrev ( r, "test" ) );
    
      return 0;
    }
    My best code is written with the delete key.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here is another variation that reverses the string in place.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *mystrrev(char *s)
    {
       char *start = s, *t = strchr(s, '\0'); /* point to end of string */
       /*
        * Swap the values at the beginning (pointed to by 's') and the end
        * (pointed to by 't'); 's' and 't' meet in the middle.
        */
       for ( --t/* skip terminating null character */; s < t; ++s, --t )
       {
          /* Just your run-of-the-mill swap here. */
          char temp = *s;
          *s = *t;
          *t = temp;
       }
       return start;
    }
    
    int main(void)
    {
       char text[] = "Hello world";
       puts(text);
       puts(mystrrev(text));
       return 0;
    }
    
    /* my output
    Hello world
    dlrow olleH
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Thanx I knew there would be a way to do it without malloc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse string without using strrev() function
    By revolution3396 in forum C Programming
    Replies: 3
    Last Post: 11-27-2008, 02:34 AM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. palindromes....
    By imbecile in C in forum C Programming
    Replies: 8
    Last Post: 08-09-2003, 05:08 PM
  4. strrev! whats wrong?
    By Silverdream in forum C Programming
    Replies: 3
    Last Post: 02-15-2002, 01:11 AM
  5. trying not to use strrev() function
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-21-2001, 03:05 PM