Last active
October 23, 2018 19:34
-
-
Save slayerlab/538b165373857a1d5529de26850bd015 to your computer and use it in GitHub Desktop.
ANSI C K&R Exercise: Print [o]ne [w]ord per [l]ine – pretty easy.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* * | |
* From: C Answer Book | |
* Clovis L. Tondo | |
* Scott E. Gimpel | |
* ISBN: 7-302-02728-5 | |
* */ | |
#include <stdio.h> | |
#define IN 1 /* inside word */ | |
#define OUT 0 /* outside word */ | |
/* print input one word per line */ | |
main() | |
{ | |
int c, state; | |
state = OUT; | |
while((c = getchar()) != EOF) { | |
if (c == ' ' || c == '\n' || c == '\t') { | |
if (state == OUT) { | |
putchar('\n'); /* finish the word */ | |
state = OUT; | |
} | |
} | |
} else if (state == OUT) { | |
state = IN; /* begining of word */ | |
putchar(c); | |
} else { | |
putchar(c); /* inside the word */ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* * | |
* Exercise 1-12. Write a program that prints its input one word per line. | |
* */ | |
#include <stdio.h> | |
#define IS_UPPER(N) ((N) >= 'A' && (N) <= 'Z') /* 'A'==65 && 'Z'==90 */ | |
#define IS_LOWER(N) ((N) >= 'a' && (N) <= 'z') /* 'a'==97 && 'z'==122 */ | |
#define IS_ALPHA(N) (IS_LOWER(N) || IS_UPPER(N)) /* [A-Za-z] x*/ | |
#define OUT 0 | |
#define IN 1 | |
int main(void) | |
{ | |
int c = EOF, state = OUT; | |
while ((c = getchar()) != EOF) { | |
if (IS_ALPHA(c)) { | |
state = IN; | |
putchar(c); | |
} | |
else if (state == IN) { | |
state = OUT; | |
putchar('\n'); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment