arrays - Reverse each word in a string using C -


ok, code works, messes end of every line. instance, if have stdin text file these 3 lines:

this test see how code messes 

output reads:

siht si tsetrof uoy ot eeswoh siht edoc sessem pu 

let me know if catch thanks

void reverse(char *beg, char *end) {   while (beg<end)   {     char temp = *beg;     *beg++ = *end;     *end-- = temp;   } }   void reversewords(char *str) {   char *beg = null;   char *temp = str;   while (*temp)   {     if ((beg == null) && (*temp != ' '))     {       beg = temp;     }     if (beg && ((*(temp + 1) == ' ') || (*(temp + 1) == '\0')))     {       reverse(beg, temp);       beg = null;     }   temp++;   } } 

new lines in code not taken consideration.

in code below i've changed occurrences of *something == ' ' call newly added method iswhitespace, returns true if character being checked either space, tab, newline, or carriage return character:

void reverse(char *beg, char *end) {   while (beg<end)   {     char temp = *beg;     *beg++ = *end;     *end-- = temp;   } }  int iswhitespace(char value) {   return value == ' ' || value == '\t' || value == '\r' || value == '\n'; }   void reversewords(char *str) {   char *beg = null;   char *temp = str;   while (*temp)   {     if ((beg == null) && !iswhitespace(*temp))     {       beg = temp;     }     if (beg && (iswhitespace(*(temp + 1)) || (*(temp + 1) == '\0')))     {       reverse(beg, temp);       beg = null;     }   temp++;   } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -