Created KNR Redux directory for re-visiting old lessons to warm back up. Implemented a solution for 1.8 and two solutions for 1.9.
This commit is contained in:
parent
8665f996e5
commit
e67db44312
4 changed files with 72 additions and 0 deletions
BIN
KNR Redux/a.out
Executable file
BIN
KNR Redux/a.out
Executable file
Binary file not shown.
25
KNR Redux/ex1-8.c
Normal file
25
KNR Redux/ex1-8.c
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int c = 0;
|
||||||
|
int whitespace = 0;
|
||||||
|
|
||||||
|
while((c = getchar()) != EOF)
|
||||||
|
switch(c){
|
||||||
|
case '\n':
|
||||||
|
++ whitespace;
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
++whitespace;
|
||||||
|
break;
|
||||||
|
case ' ':
|
||||||
|
++whitespace;
|
||||||
|
break;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%d %c", whitespace, '\n');
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
22
KNR Redux/ex1-9-2.c
Normal file
22
KNR Redux/ex1-9-2.c
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int c = 0;
|
||||||
|
_Bool lastWS = 0;
|
||||||
|
|
||||||
|
while((c = getchar()) != EOF){
|
||||||
|
if(c == '\t' || c == ' '){
|
||||||
|
if(lastWS == 0){
|
||||||
|
lastWS = 1;
|
||||||
|
putchar(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
lastWS = 0;
|
||||||
|
putchar(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
25
KNR Redux/ex1-9.c
Normal file
25
KNR Redux/ex1-9.c
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int c = 0;
|
||||||
|
_Bool lastWS = 0;
|
||||||
|
|
||||||
|
while((c = getchar()) != EOF){
|
||||||
|
//Check if the current character is a whitespace
|
||||||
|
if(c == '\t'|| c == ' '){
|
||||||
|
if(lastWS == 0){
|
||||||
|
//If the last character was not also WS, set lastWS 1 and print
|
||||||
|
lastWS = 1;
|
||||||
|
putchar(c);
|
||||||
|
}//If c is WS and last was WS, statement fails.
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//If not WS, set lastWS 0 and print.
|
||||||
|
lastWS = 0;
|
||||||
|
putchar(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue