Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
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; i >= 0; i--) {
		strncpy(charAtIndex, &str[i], 1);
		result = strcat(dest, revChar(charAtIndex));
	}
	return result;
}

int main () {
	char text[] = "striNg";
	printf("Text: %s\n", text);
	printf("reverseText: %s\n", revstr(text));
	return 0;
}
 
Hello,

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

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?
 
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;
}
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.