-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentances.c
More file actions
46 lines (41 loc) · 990 Bytes
/
sentances.c
File metadata and controls
46 lines (41 loc) · 990 Bytes
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
//
// A simple command line program to read in sentances,
// generate a ragged array where each word (space delimited) is
// commited to an element of the array and the length is recorded
//
// output each word on a seperate line
//
#include <stdio.h>
#define MAX_LEN 80
int main(void){
// set up character array, it will be ragged
char ch[MAX_LEN][MAX_LEN];
// request STDIN input up to 80 characters
printf("Input a sentance:\n");
int len[MAX_LEN] = {0};
int j = 0;
int i = 0;
while(1){
ch[i][j] = getchar();
if (ch[i][j] == ' '){
len[i]= j+1;
ch[i][j+1] = '\0';
i++;
j = 0;
}
else if ((ch[i][j] != '\n') && (ch[i][j] !=' ')) j++;
else if (ch[i][j] == '\n'){
ch[i][j] = '\0';
len[i] = j+1;
break;
}
else ;
}
printf("\n");
for (int q = 0; q <= i ; q++){
// remove one character off length for null terminator
printf("%s\t:\t%d\n",ch[q],len[q]-1);
}
printf("\n");
return 0;
}