Ankur said:
I don't think it's possible in one pass. But if you mean the above
literally, it's quite easy to do it in /two/ passes, by simple
counting.
#include <stddef.h>
#include <assert.h>
void sortrgb(char *s, size_t len)
{
unsigned long rcount = 0;
unsigned long gcount = 0;
unsigned long bcount = 0;
char *start = s;
while(*s != '\0')
{
switch(*s)
{
case 'r': ++rcount; break;
case 'g': ++gcount; break;
case 'b': ++bcount; break;
default: assert(0); /* assumptions are wrong */
}
++s;
}
while(rcount--)
{
*start++ = 'r';
}
while(gcount--)
{
*start++ = 'g';
}
while(bcount--)
{
*start++ = 'b';
}
assert(*start == '\0'); /* if I got the above right,
this assertion will not fire */
return;
}
--
Richard Heathfield <
http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <
http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999