Reversing a string in C

Discussion in 'Mac Programming' started by rbert, Apr 7, 2010.

  1. rbert macrumors newbie

    Joined:
    Nov 15, 2009
    #1
    Hello,

    I am a newbee. I would like to reverse a string in C. I got 2 problems but i do not know why:

    1. For a input string "striNg" i got the reverseText result "0gZirts". This is correct, but why i always receive the "0" at the beginning?
    2. For a input string "striNg for fun" i got a total wrong result "0nuf rof gZy/\225d\367\377\377\277".

    Can you please help me. Thank you!


    PHP:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    char *revChar(char *chr) {
        if (*
    chr == 'N') {
            
    // Replace N with Z
            
    char *chr "Z";
            return 
    chr;
        }
        return 
    chr;
    }

    char *revstr(char *str) {
        
    size_t length strlen(str);
        
    char dest[length];
        
    char charAtIndex[length];
        
    char *result;
        
        for (
    int i length 1>= 0i--) {
            
    strncpy(charAtIndex, &str[i], 1);
            
    result strcat(destrevChar(charAtIndex));
        }
        return 
    result;
    }

    int main () {
        
    char text[] = "striNg";
        
    printf("Text: %s\n"text);
        
    printf("reverseText: %s\n"revstr(text));
        return 
    0;
    }
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    A char* is just a pointer. When you return "result", you just return a pointer. Where does that pointer point to? Where is the memory that it points to? What happens to that memory when the function revstr returns? Where do you think the reversed characters are stored?

    I think you should first try to write a function

    void revstr (char* str);

    which rearranges the characters that str points to, without trying to copy them anywhere else. So when you call revstr (text), the characters in the array text would be rearranged. Even better would be if you first write a spec for the function, which says _exactly_ what it does because I'm not quite sure you know what it is supposed to do.

    BTW. What happens when you call strcat and the destination is uninitialised memory?
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    Deja-vu ...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char* reverse(char string[])
    {
        char*   lhs   = &string[0]-1;
        char*   rhs   = &string[strlen(string)];
        char    temp;
        while ( ++lhs < --rhs )
        {
            temp = *lhs;
            *lhs = *rhs;
            *rhs = temp;
        }
        
        return string;
    }
    
    int main(int argc, char* const argv[])
    {
        const char* psz = "constant - reverse this string!";
        char str[]      = "reverse this string!";
        
        printf("%s\n", str);
        printf("%s\n", reverse(str));
        
    #if 0
        // --- error, can't modify a string constant in-place
        printf("%s\n", psz);
        printf("%s\n", reverse(psz));
    #endif  
    
        char*   pszTemp = (char*)calloc(strlen(psz) + 1, sizeof(char));
        strcpy(pszTemp, psz);
    
        printf("%s\n", pszTemp);
        printf("%s\n", reverse(pszTemp));
    
        free(pszTemp);
    
        return 0;
    }
    
     
  4. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #4
    char array strings are null terminated. That means that the last character in the array is the null terminator. Without using the string.h functions, you should keep in mind that the last character in the array should stay in place. Just reverse every other character.
     

Share This Page