-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeat.c
More file actions
48 lines (35 loc) · 1.18 KB
/
repeat.c
File metadata and controls
48 lines (35 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 2026 S1C COMP2017 Week 2 Tutorial A & B
// Tutor: Hao Ren (hao.ren@sydney.edu.au)
// Demonstration for `getchar()`, `scanf()` and `fgets()`.
#include <stdio.h>
#include <string.h> // Only required by Solution 3
//------------------------------------------------------------------------------
// Solution 1 by using getchar()
int main(void) {
int c; // int is required so we can represent all chars + EOF
while ((c = getchar()) != EOF) {
if (putchar(c) == EOF) { // write to stdout
return 1;
}
}
return 0;
}
// -----------------------------------------------------------------------------
// Solution 2 by using scanf()
// int main(void) {
// char line[100];
// // Reads up to 99 chars that are not '\n'
// if (scanf("%99[^\n]", line) == 1) {
// printf("%s\n", line);
// }
// return 0;
// }
// -----------------------------------------------------------------------------
// Solution 3 by using fgets()
// int main(void) {
// char buf[1024];
// while (fgets(buf, sizeof buf, stdin)) {
// buf[strcspn(buf, "\n")] = '\0'; // remove trailing newline if any
// printf("%s\n", buf);
// }
// }