Need help on simple problem
Author |
Message |
Matt Rickar #1 / 11
|
 Need help on simple problem
I'm working through K+C 2nd edition, and I'm trying to do exercise 1-9 now. I want to write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. Here is my attempt at the program. It almost works, but when I enter more than 2 spaces consecutively, I start running into trouble as it doesn't bring it back to only 1 space. I'm sure it is far from optimal, so please give me some input on how to better write the code, and how to properly get this program to function. Thanks. #include <stdio.h> int main(void) { int c, blank; blank = 0; while((c = getchar()) != EOF) { if (c == ' ') ++blank; if (blank >= 2) { c = getchar(); putchar(c); blank = 0; } else putchar(c); } return 0; Quote: }
|
Tue, 22 Jul 2003 08:17:07 GMT |
|
 |
Stefan Farfeled #2 / 11
|
 Need help on simple problem
Quote:
> I'm working through K+C 2nd edition, and I'm trying to do exercise >1-9 now. I want to write a program to copy its input to its output, >replacing each string of one or more blanks by a single blank. >Here is my attempt at the program. It almost works, but when I enter >more than 2 spaces consecutively, I start running into trouble as it >doesn't bring it back to only 1 space. I'm sure it is far from optimal, >so please give me some input on how to better write the code, and how to >properly get this program to function. Thanks. >#include <stdio.h> >int main(void) >{ > int c, blank; > blank = 0; > while((c = getchar()) != EOF) > { > if (c == ' ') > ++blank; > if (blank >= 2) > { > c = getchar(); > putchar(c); > blank = 0; > } > else > putchar(c); > } >return 0; >}
The problem is way easier than you think it is. There is no need to count spaces, all you have to do is to save the char read before the current and do the following: 1. If the previous char was not a space, print it always. 2. Else print the current char only if it is *not* a space. I would write it like this #include <stdio.h> int main(void) { int prev = '\0'; int ch; while ((ch = getchar()) != EOF) { if (prev != ' ' || ch != ' ') { putchar(ch); } prev = ch; } return 0; Quote: }
Cheers, Stefan
|
Tue, 22 Jul 2003 09:20:04 GMT |
|
 |
Matt Rickar #3 / 11
|
 Need help on simple problem
Quote: > The problem is way easier than you think it is....
That's what I needed. Looks like I was just approaching the problem the wrong way. Thanks for the help. Matt
|
Tue, 22 Jul 2003 11:22:26 GMT |
|
 |
Nick Keighle #4 / 11
|
 Need help on simple problem
Quote: > I'm working through K+C 2nd edition
rather than the more well known K&R... :-) Sent via Deja.com http://www.deja.com/
|
Tue, 22 Jul 2003 22:12:53 GMT |
|
 |
Gregory Pietsc #5 / 11
|
 Need help on simple problem
Quote:
> I'm working through K+C 2nd edition, and I'm trying to do exercise > 1-9 now. I want to write a program to copy its input to its output, > replacing each string of one or more blanks by a single blank. > Here is my attempt at the program. It almost works, but when I enter > more than 2 spaces consecutively, I start running into trouble as it > doesn't bring it back to only 1 space. I'm sure it is far from optimal, > so please give me some input on how to better write the code, and how to > properly get this program to function. Thanks. > #include <stdio.h> > int main(void) > { > int c, blank; > blank = 0; > while((c = getchar()) != EOF) > { > if (c == ' ') > ++blank; > if (blank >= 2) > { > c = getchar(); > putchar(c); > blank = 0; > } > else > putchar(c); > } > return 0; > }
#include <stdio.h> #include <ctype.h> #define A int #define F EOF #define G (c=getchar()) #define I(x,y) (y)?(i=x,putchar(c)):0 #define J if #define K while #define M A main(void) #define N A c,i=0 #define R return 0 #define S isspace(c) M{N;K(G!=F){J(S)I(1,!i);I(0,!S);}R;}
|
Wed, 23 Jul 2003 09:46:17 GMT |
|
 |
ny ch #6 / 11
|
 Need help on simple problem
Hi Matt! My reply below in black.
Quote: > I'm working through K+C 2nd edition, and I'm trying to do exercise >1-9 now. I want to write a program to copy its input to its output, >replacing each string of one or more blanks by a single blank. >Here is my attempt at the program. It almost works, but when I enter >more than 2 spaces consecutively, I start running into trouble as it >doesn't bring it back to only 1 space. I'm sure it is far from optimal, >so please give me some input on how to better write the code, and how to >properly get this program to function. Thanks. >#include <stdio.h> >int main(void) >{ > int c, blank; > blank = 0; > while((c = getchar()) != EOF) > { > if (c == ' ') > ++blank;
I changed this to blank++ and it seemed to work okay. Quote: > if (blank >= 2) > { > c = getchar(); > putchar(c); > blank = 0; > } > else > putchar(c); > } >return 0; >}
|
Mon, 28 Jul 2003 19:39:50 GMT |
|
 |
Z #7 / 11
|
 Need help on simple problem
Quote: > Hi Matt! My reply below in black.
Dear Mr. ny chic, would you mind to explain me what you mean by black? Are you by chance trying to troll around in here? --
"LISP is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days." -- Eric S. Raymond
|
Mon, 28 Jul 2003 20:54:03 GMT |
|
 |
Torbj?rn Gyllebrin #8 / 11
|
 Need help on simple problem
here's one way to accomplish that task. [code] #include <stdio.h> main() { int c; while(EOF != (c = getchar())) { putchar(c); if(c == ' ') for(;;) if(' ' != (c = getchar())) { putchar(c); break; } } return 0; Quote: }
[/code]
|
Mon, 28 Jul 2003 22:04:27 GMT |
|
 |
Richard B #9 / 11
|
 Need help on simple problem
Quote:
> > Hi Matt! My reply below in black. > Dear Mr. ny chic, > would you mind to explain me what you mean by black? > Are you by chance trying to troll around in here?
No, merely extremely newbie, I suspect. ny chic (whom I suspect is not a Mr., because of the chic), uses Forte Free Agent, and in that newsreader, the default for non-quoted material _is_ black. In many others, it is not, or there is no colour difference between quoted and unquoted material. And what do we learn from all this? "That the whole world is not the same as our bitty box, Mr. Bos!" Correct, my children. And what does that mean? "That something we see as black on our screen might not show that way on our readers' screens, Mr. Bos!" Indeed, my children. Oh, and ny chic, please refrain from top-posting. It disturbs the flow of the thread, and can confuse people who read more than one thread a day. Thank you. Richard
|
Mon, 28 Jul 2003 22:05:41 GMT |
|
 |
CBFalcone #10 / 11
|
 Need help on simple problem
Quote:
> > I'm working through K+C 2nd edition, and I'm trying to do exercise > >1-9 now. I want to write a program to copy its input to its output, > >replacing each string of one or more blanks by a single blank. > >Here is my attempt at the program. It almost works, but when I enter > >more than 2 spaces consecutively, I start running into trouble as it > >doesn't bring it back to only 1 space. I'm sure it is far from optimal, > >so please give me some input on how to better write the code, and how to > >properly get this program to function. Thanks. > >#include <stdio.h> > >int main(void) > >{ > > int c, blank; > > blank = 0; > > while((c = getchar()) != EOF) > > { > > if (c == ' ') > > ++blank; > I changed this to blank++ and it seemed to work okay. > > if (blank >= 2) > > { > > c = getchar(); > > putchar(c); > > blank = 0; > > } > > else > > putchar(c); > > } > >return 0; > >}
IF we assume the objective is to read stdin and convert all strings of whitespace to single blanks on stdout, then try the following modification: #include <stdio.h> int main(void) { int c, blank; blank = 0; while (EOF != (c = getchar())) { if ((' ' == c) || ('\t' == c)) ++blank; else { /* note the else */ if (blank && ('\n' != c)) putchar(' '); /* suppress trailing blanks */ putchar(c); blank = 0; } } return 0; Quote: }
This includes tabs in whitespace. --
http://www.qwikpages.com/backstreets/cbfalconer (Remove "NOSPAM." from reply address. my-deja works unmodified)
|
Tue, 29 Jul 2003 02:32:28 GMT |
|
 |
Tor Rusta #11 / 11
|
 Need help on simple problem
[...] Please fix your OE "SV:" problem, the correct way is to start the subject line with "Re:". It looks like Microsoft did this mistake with OE in all the Scandinavian countries. It irritate me that OE translate "-- " into "--", but for this I don't know any workaround. -- Tor <torust AT online DOT no>
|
Wed, 30 Jul 2003 18:22:55 GMT |
|
|
|