1
votes

I'm writing a program in which a function that reverses each word in a string. When I call the function, it will pass the pointer to source string and then return the pointer to modified string.

input: Why always me? output: yhW syawla ?em

But for some reason, it is not working. No errors. And logic seemed fine to me (i'm not that good with c, btw) Here's my code:

char *reverse_words(char *source_string)
{
    char *startW, *endW;
    startW = source_string;

    while(*startW != '\0')
    {
        while(*startW == ' ')
        startW++;       //Skip multiple spaces in the beginning

        endW = startW;
        while(*endW != ' ' || *endW != '\0')
        endW++;

        char *_start = startW;
        char *_end = endW - 1;
        char temp;

        while(_end > _start)
        {
            temp = *_start;
            *_start++ = *_end;
            *_end++ = temp;
        }

        startW = endW;
    }

    return source_string;
}

void main() {
    char *s;
    s = malloc(256);
    gets(s);
    printf("%s\n", s);
    char *r = reverse_words(s);
    printf("\nReversed String : %s",r);
    free(s);
    free(r);
}

Also, i'm using codeblocks IDE. After I input my string, it prints it back (scanf and printf in main) and after that, the program stops working.

Any help would be appreciated.

3
for start, rev=source_string just copy the reference and not actual stringGoldRoger
Read this: ericlippert.com/2014/03/05/how-to-debug-small-programs and then try to ask a more specific, focused question. "I don't know how to debug a program I wrote" is not an answerable question, and this isn't a service for doing your debugging for you.Eric Lippert

3 Answers

2
votes

First,

    while(*endW != ' ' || *endW != '\0')

is an infinite loop, try this instead:

    while(*endW != ' ' && *endW != '\0')

Second,

        *_end++ = temp;

should be this:

        *_end-- = temp;
0
votes

In the innermost while(_end > _start) loop you increment both _start and _end. So the condition will never become false. (Well, not until _end overflows.)

I'd recommend figuring out how to do step-by-step debugging in your IDE. Then you can easily understand what exactly goes wrong in a case like this, without simulating the execution in your head.

0
votes
#include <stdio.h>
#include <stdlib.h>

char *reverse_words(const char *source_string){
    const char *startW, *endW;
    char *p, *rev = malloc(256);
    startW = source_string;
    p = rev;

    while(*startW != '\0'){
        while(*startW == ' ')
            *p++ = *startW++;
        if(!*startW)
            break;

        endW = startW;
        while(*endW != ' ' && *endW != '\0')
            endW++;
        const char *endW2 = endW;
        do{
            *p++ = *--endW;
        }while(startW!=endW);
        startW = endW2;
    }
    *p = '\0';
    return rev;
}

int main() {
    char s[256];
    scanf("%255[^\n]", s);
    printf("%s,\n", s);
    char *r = reverse_words(s);
    printf("\nReversed String : %s.", r);
    free(r);
    return 0;
}