My try inspired by bitingduck's triple reverse: #include <stdio.h>
#include <string.h>
void reverse( char*p, int n ) {
char c, *e = p+n-1;
n /= 2;
while( n-- ) c = *p, *p++ = *e, *e-- = c;
}
char *reverseWords( char*line ) {
int len = (int)strlen( line );
char *p = line, *p1 = line, *e = line+len;
reverse( line, len );
while( p <= e ) {
if( *p == 32 || *p == 0 ) {
reverse( p1, (int)( p - p1) );
p1 = p+1;
}
++p;
}
return line;
}
int main( int argc, char **argv ) {
char line[ 1024 ];
printf( "%s\n", reverseWords( gets_s( line, 1024 ) ) );
return 0;
}
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|