Thread: String Reverse in C

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    5

    Question String Reverse in C

    Hello guys,

    Sorry for my bad english I'm from Brazil, let's go...
    I was trying to create a code that reverses the string in C, but I don't understand why that code does not work.

    Can you guys help me?
    Thanks a lot!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char* reversestring(char* stringt){
        int len = strlen(stringt);
        char *ret = malloc(len * sizeof(char));
        int i = 0;
        int count;
        for(count = len;count > 0;--count){
            ret[i++] = stringt[count];
            
        }
        return ret;
        free(ret);
    }
    
    
    int main(){
        printf("%s",reversestring("teste"));
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    The first char you are copying is the '\0' that terminates stringt (i.e., stringt[len] is '\0'). Then you copy all the other characters (and forget to put a '\0' at the end). So with the '\0' at the beginning, the result is interpretted as an empty string and nothing gets printed.

    You also don't malloc enough space. It needs to be len + 1 to include the terminating '\0' char (which is not counted by strlen). And you need to add the '\0' after the loop.

    BTW, your free() call will never happen since the return before it will tranfer control back to main. The free() needs to be in main anyway.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    char *reversestring(const char *str){
        int len = strlen(str);
        char *ret = malloc(len + 1);
        int i = 0;
        for(int j = len; j-- > 0; )
            ret[i++] = str[j];
        ret[i] = '\0';
        return ret;
    }
     
    int main(){
        char *s = reversestring("teste");
        printf("%s\n", s);
        free(s);
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    5
    I understand now...

    Thank you very much.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    I find your explanation straight to the point and very enlightening. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse a string without string.h library
    By antros48 in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 06:01 PM
  2. reverse a string
    By jas_atwal in forum C Programming
    Replies: 5
    Last Post: 01-01-2008, 01:16 PM
  3. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  4. How to reverse a string C
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 07-18-2002, 12:26 PM
  5. reverse string
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2001, 02:16 PM

Tags for this Thread