-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuggy.c
More file actions
58 lines (53 loc) · 1.47 KB
/
buggy.c
File metadata and controls
58 lines (53 loc) · 1.47 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
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORDS 80
#define MAX_LINE 800
void tokenize(char *input, char *words[], int maxlen);
void reverse_words(char *words[]) {
// compute length of words; swap words[i] with words[length-1-i]
// MY BUG!
// int wordslen = strnlen(words[0], MAX_LINE);
int wordslen = 0;
while (words[wordslen] != NULL) {
wordslen++;
}
printf("wordslen = %d\n", wordslen);
for (int i = 0; i < wordslen; i++) {
printf("words[%d]: %s\n", i, words[i]);
words[i] = words[wordslen - 1 - i];
words[wordslen - 1 - i] = words[i];
}
}
int main(int argc, char **argv) {
char *words[MAX_WORDS];
char input_buffer[MAX_LINE];
// read a line
printf("string? > ");
char *ret = fgets(input_buffer, MAX_LINE, stdin);
while (ret != NULL) {
// while we have input, tokenize the line and then reverse the individual
// words
tokenize(input_buffer, words, MAX_WORDS);
reverse_words(words);
// print the reversed words
printf("reversed: ");
for (int i = 0; words[i] != NULL; i++) {
printf("%s ", words[i]);
}
printf("\n");
// read next line
printf("string? > ");
ret = fgets(input_buffer, MAX_LINE, stdin);
}
}
void tokenize(char input[], char *words[], int maxlen) {
memset(words, 0, sizeof(char *) * maxlen);
int c = 0;
words[c] = strtok(input, " ");
while (words[c] != NULL && c < maxlen) {
c++;
words[c] = strtok(NULL, " ");
}
words[maxlen - 1] = NULL;
}