]> zdv2.bktei.com Git - BK-2023-05.git/blob - src/kr_exercises/ch1/s1.9/array_test.c
feat(src/kr/ch1/s1.1/p6):Add hello world example
[BK-2023-05.git] / src / kr_exercises / ch1 / s1.9 / array_test.c
1 #include <stdio.h>
2
3 #define MAXLINE 1000
4
5 int mygetline(char line[], int maxline); /* 'my-' to avoid name collision */
6
7 int main() {
8 int len;
9 char line[MAXLINE];
10
11 while( (len=mygetline(line, MAXLINE)) > 0 ) {
12 line[0]=64; // '@'
13 printf("%s\n", line);
14 };
15
16 return 0;
17 };
18
19 /* mygetline: read a line into s, \0-term, return length v2 */
20 int mygetline(char s[], int lim) {
21 int c, i, k, j;
22
23 /* Read chars into s[] until lim reached */
24 for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i )
25 s[i] = c;
26 j = i; /* Store char count for counting beyond lim */
27
28 /* \0 terminate array s[] */
29 if (c=='\n') {
30 s[i] = c;
31 ++i;
32 };
33 s[i] = '\0';
34
35 /* Count up remaining chars beyond lim until EOF or '\n' reached */
36 if (j>=lim-1) {
37 while ( (c=getchar()) != EOF && c != '\n')
38 ++j;
39 };
40 if (c=='\n')
41 ++j;
42
43 /* Return char count (including chars beyond array limit)*/
44 return j;
45 };