C
Christos Kokaliaris
Greetings everybody,
I am a Mathematics graduate and I need C in order to integrate it with R and other languages I use for statistics and simulation. I took C when undergraduate but it wasn't enough. Recently I decided to study analytically the classic book and solve every single exercise. After comparing my own codewith the already suggested in the archives of clc-wiki, I thought of posting my solutions.
Criticism and suggestions on the quality of the solutions I suggest are much needed and most welcome.
Thank you for reading.
// ex1-9
#include <stdio.h>
/* copy input into output, replacing
one or more blanks by a single blank */
main()
{
int c, nb=0;
while ((c=getchar()) != EOF) {
/* if char is blank increase counter else reset counter.
this notation will be mentioned later in the book
(T) ? A : B; if TRUE then A else B */
(c == ' ') ? (++nb) : (nb = 0);
if (c != ' ' || nb < 2) putchar(c);
}
}
//ex1-12
#include <stdio.h>
#define IN 1
#define OUT 0
// print input one word per line
main()
{
int c, state;
// start without a word
state = OUT;
while ((c = getchar()) != EOF) {
// if the char is not blank, tab, newline
if (c != ' ' && c != '\t' && c != '\n') {
// inside a word
state = IN;
putchar(c);
}
// otherwise char is blank, tab, newline, word ended
else if (state == IN) {
state = OUT;
putchar('\n');
}
}
}
I am a Mathematics graduate and I need C in order to integrate it with R and other languages I use for statistics and simulation. I took C when undergraduate but it wasn't enough. Recently I decided to study analytically the classic book and solve every single exercise. After comparing my own codewith the already suggested in the archives of clc-wiki, I thought of posting my solutions.
Criticism and suggestions on the quality of the solutions I suggest are much needed and most welcome.
Thank you for reading.
// ex1-9
#include <stdio.h>
/* copy input into output, replacing
one or more blanks by a single blank */
main()
{
int c, nb=0;
while ((c=getchar()) != EOF) {
/* if char is blank increase counter else reset counter.
this notation will be mentioned later in the book
(T) ? A : B; if TRUE then A else B */
(c == ' ') ? (++nb) : (nb = 0);
if (c != ' ' || nb < 2) putchar(c);
}
}
//ex1-12
#include <stdio.h>
#define IN 1
#define OUT 0
// print input one word per line
main()
{
int c, state;
// start without a word
state = OUT;
while ((c = getchar()) != EOF) {
// if the char is not blank, tab, newline
if (c != ' ' && c != '\t' && c != '\n') {
// inside a word
state = IN;
putchar(c);
}
// otherwise char is blank, tab, newline, word ended
else if (state == IN) {
state = OUT;
putchar('\n');
}
}
}