John,
Very good, but you worked with an array of char's & a pointer to char's: two
strings & one a copy of the other, a sort of a crutch, he, he, he.
Came to a different solution using only the original string & the fundamentals
of the language.
char *s=" C Programming Language ";
int f1=1, f2=1;
while (*s) {
if (*s!=' ') {if (f1) printf("%c", *s); f1=0;}
else
if (!f1) {printf("%c", *s); f2=0;}
if (!f1 & !f2) f1=f2=1;
++s;
}
The backspace at the end is optional, depending on how formalist you are, if
the string has trailing blanks, but isn't necessary if it contains only blanks
or is empty; it's easy to see why: f1 won't ever be zero.
Happy coding & decoding!
Geraldo
PS: Hope to find time now for my cherished ancient Egyptian multiplication and
division [program] that I began a couple of days ago for fun & didn't bring to
an end yet for absolute lack of time.
--- In [email protected], "johnmatthews2000" <jm5...@...> wrote:
>
> --- In [email protected], "py2akv" <gdb@> wrote:
> >
> > John,
> >
> > You know a picture is worth a thousand words.
> >
> > Show us your one-loop-only code so that everyone can review/comment on it.
>
> Oh, if you insist :-)
>
> #include <stdio.h>
>
> int main(void)
> {
> const char *s, str[] = " C Programming Language ";
> int n;
>
> for (n = 0, s = str; *s; s++)
> if ((*s != ' ') && ((s == str) || (s[-1] == ' ')))
> printf(" %c" + !n++, *s);
>
> putchar('\n'); /* might not be required */
>
> return 0;
> }
>
> The mechanism for not outputting a leading is compact rather than
> comprehensible, but could be modified eg.
>
> printf(n++ ? " %c" : "%c", *s);
>