HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Reverse each word of a string in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
wordstringeachreverse

Problem

I had this interview question like a year ago and was asked to code, on a piece of paper, how to reverse each word of a string. Since I am used to Java, I proposed the obvious answer of using split + reverse, which are native commands in Java. I was then told I couldn't use those, so I floundered and ended up with a really terrible solution (even though it technically would've worked).

Anyway, it was bugging me lately, so I gave it a shot in straight C, which I am not very good at, so it took me a good while to actually get it working.

I was wondering:

  • Is this a good solution?



  • Have I forgotten anything obvious?



  • Have I done anything non-kosher in the C world?



Again, I'm not very good at C, so even small points will probably help me out.

#include 

void reverseString(char* start, char* end){
   while (start < end){
      char temp = *start;
      *start = *end;
      *end = temp;
      ++start;
      --end;
   }   
}

char* word_start_index(char* p)
{
    while((*p != '\0') && (*p == ' ')){
        ++p;    
    }

    if(*p == '\0')
        return NULL;
    else
        return p;
}

char* word_end_index(char* p)
{
    while((*p != '\0') && (*p != ' ')){
        ++p;
    }

    return p-1;
} 

void main(){
    char arr[] = "kevin is a good programmer";
    char* test = arr;

    while (test != '\0'){
        char* curWordStart = word_start_index(test);
        if (curWordStart == NULL)
            break;
        char* curWordEnd = word_end_index(curWordStart);
        reverseString(curWordStart, curWordEnd); 
        test = curWordEnd + 1;
    }
    printf("%s \n", arr);
}


Also, would taking a different approach, like in higher level languages of breaking the string into an array of strings (so I guess a 2D array of chars), then stepping through and reversing each one, be a good approach as well? I thought about this first and was unable to hash it out.

Solution

I have a few issues with in your code:

-
Consistency in naming - use either camelCase or not_camel_case but don't
mix

-
consistency in braces. The opening brace for a function goes in the first
column.

-
word_start_index and word_end_index should take a const parameter

-
word_start_index is the same as strspn(string, " "); or if you are also
looking for punctuation, strspn(string, " \t\n.,;:");

-
word_end_index - as for word_start_index but use strcspn (note the
'c')

-
word_end_index as a function (ie not in your context) fails for an empty
string or a string starting with a space (it returns the char before the
string starts).

-
variable test in main() is misnamed. I would prefer something that shows it
is a string.

-
the test while (test != '\0') in main() is wrong - should be *test !=
'\0'
.
Your loop always exits from the break

-
no return or parameters in main()

Also, arguably the position of the stars in your pointers is wrong. I prefer char p to be written char p, which makes it clear that it is p that takes the star. Consider code such as char* a, b;. This is bad because it gives the impression that b is a pointer.

Context

StackExchange Code Review Q#18229, answer score: 6

Revisions (0)

No revisions yet.