Skeleton prototype for exercise 1-13.
This commit is contained in:
parent
dbf602e13d
commit
4b03274352
1 changed files with 41 additions and 0 deletions
41
KNR C/ex1_13.c
Normal file
41
KNR C/ex1_13.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define IN 1 /*State when selected character is within a word*/
|
||||
#define OUT 0 /*State when selected character is any kind of whitespace*/
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int charCount[10]; /*Array for storing char lengths*/
|
||||
|
||||
int last, lCount, state, i; /*last stores last character, or 0 at init. lCount increments every time the main for loop encounters a non-newline/whitespace character and then resets when one is encountered after commiting its value to charCount.*/
|
||||
/*Begin array initialization*/
|
||||
for(i = 0; i < 10; ++i){
|
||||
charCount[i] = 0;
|
||||
}
|
||||
/*End array init*/
|
||||
|
||||
|
||||
last = lCount = 0;
|
||||
|
||||
for(int a = getchar(); a != EOF; a = getchar()){
|
||||
|
||||
if(a == '\t' || a == ' ' || a == '\n'){ /*Check if current char is WS. If so, set state to OUT and check if the last character was not. If it was not, then we are exiting a word, and so comitting lCount to charCount.*/
|
||||
state = OUT;
|
||||
if (last != '\t' && last != ' ' && last != '\n'){ /*True on word exit only.*/
|
||||
++charCount[lCount-'0'];
|
||||
lCount = 0;
|
||||
}
|
||||
|
||||
} else {
|
||||
state = IN;
|
||||
++lCount; /*Increment lCount when encountering a non-WS character.*/
|
||||
}
|
||||
last = a;
|
||||
}
|
||||
printf("Word letter count composition:\n1 2 3 4 5 6 7 8 9 10\n");
|
||||
for (i = 0; i < 10; ++i)
|
||||
printf("%d ", charCount[i]);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue