Advertisement

07.18.2005 at 07:34AM PDT, ID: 21495195
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Reverse words in sentence while keeping position of white space

Tags: reverse, words, sentence
Hi guys, something i couldn't figure out....

Write a function to reverse the order of words

Given a sentence of words in a character array, write a C function to reverse the order of words.

char* reverse_word (char* str);

The function gets a character array as its sole argument, reverses the order of words in it, and puts back the result into it. It returns the pointer to the argument on termination for convenience sake.

See the following example.
char str[] = "this     is very beautiful   ";
printf ("[%s]\n", reverse_word(str));   /* print [beautiful     very is this   ] */

Be ware that the position of space characters has not moved; the result of the example above is [beautiful     very is this   ], not [beautiful very is     this   ].

The following constraints apply:
No external function calls. (strlen, strcpy, etc).
No explicit memory allocation. (malloc, realloc, dynamic arrays, etc)

I managed only managed to reverse the order of words which results in [beautiful very is     this   ], however I didn't know how to maintain the position of space characters [beautiful     very is this   ].

this is the code that i wrote:

#include <stdio.h>
char* reverse_word(char* s)
{
    char *str = s;
    int len = string_len(s);
    int i=0;
    int count=0;
   
    reverse_substring(str,0,pos_lastChar(str));
    while(i<=len){
        if(str[i] ==' ' || i==len){
            reverse_substring(str,i-count,i-1);
            count=0;    
        }  
        if(str[i]!=' '){
            count++;
        }
        i++;
    }
    return str;
}

int pos_lastChar(char* s)
{
    int i;
    for(i=string_len(s); i>0;i--){
        if (s[i]!=' ' && s[i]!='\0'){
            return i;            
        }
    }
}

reverse_substring(char* s, int start, int end)
{
    char temp;
    int i,j;
    for(i=start, j=end; i<j; i++,j--)
    {
        temp = s[i];
        s[i]=s[j];
        s[j]=temp;
    }
}    
int string_len(char* str)
{
    int x=0;
    while(*str++){
        x++;
    }
    return (x);
}

int main(int argc, char **argv)
{
    char str[] = "this is   very beautiful   ";
    printf("Length of string: %d\n",string_len(str));
    printf("[%s]\n", reverse_word(str));
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: maximteo
Solution Provided By: slyong
Participating Experts: 6
Solution Grade: A
Views: 240
Translate:
Loading Advertisement...
07.18.2005 at 08:19AM PDT, ID: 14466967

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.18.2005 at 08:39AM PDT, ID: 14467206

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.18.2005 at 08:50AM PDT, ID: 14467345

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.18.2005 at 08:00PM PDT, ID: 14472181

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.18.2005 at 08:54PM PDT, ID: 14472321

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.19.2005 at 08:33AM PDT, ID: 14476077

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.19.2005 at 08:54AM PDT, ID: 14476367

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.19.2005 at 11:13AM PDT, ID: 14477928

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.19.2005 at 01:55PM PDT, ID: 14479498

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.19.2005 at 05:39PM PDT, ID: 14480760

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.19.2005 at 06:21PM PDT, ID: 14480923

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.19.2005 at 07:55PM PDT, ID: 14481259

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.20.2005 at 01:00AM PDT, ID: 14482207

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.20.2005 at 03:16AM PDT, ID: 14482883

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.20.2005 at 03:18AM PDT, ID: 14482893

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.20.2005 at 04:35AM PDT, ID: 14483283

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
07.28.2005 at 12:22PM PDT, ID: 14549114

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080236-EE-VQP-29