-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise11.c
197 lines (162 loc) · 3.71 KB
/
exercise11.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// C Primer Plus
// Chapter 11 Exercise 11:
// Write a program that reads in up to 10 strings or to EOF, whichever comes
// first. Have it offer the user a menu with five choices: print the original
// list of strings, print the strings in ASCII collating sequence, print the
// strings in order of increasing length, print the strings in order of the
// length of the first word in the string, and quit. Have the menu recycle
// until the user enters the quit request. The program, of course, should
// actually perform the promised tasks.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define COUNT 10
#define LIMIT 50
void sort_ASCII(char *strings[], int n);
void sort_length(char *strings[], int n);
int fwlen(char *string);
void sort_firstword_length(char *strings[], int n);
char * get(char *string, int n);
void print_menu(void);
int main (void)
{
char strings[COUNT][LIMIT];
char *strptrs[COUNT];
char * success;
char ch;
// initialize array of pointers
for (int i = 0; i < COUNT; i++)
strptrs[i] = strings[i];
printf("Enter up to 10 strings (EOF to stop): \n");
// read up to ten strings from input
for (int i = 0; i < COUNT; i++)
{
printf("%d: ", i + 1);
success = get(strings[i], LIMIT);
// if EOF encountered, stop reading strings
if (!success)
break;
}
printf("\n");
print_menu();
while ((ch = getchar()) != 'q')
{
// discard rest of the line
if (ch != '\n')
while (getchar() != '\n')
continue;
// sort strings
switch (ch)
{
case ('a'):
sort_ASCII(strptrs, COUNT);
break;
case ('l'):
sort_length(strptrs, COUNT);
break;
case ('f'):
sort_firstword_length(strptrs, COUNT);
break;
case ('o'):
break;
default:
printf("Invalid input. Try again.\n\n");
print_menu();
continue;
}
// print sorted strings
puts("");
for (int i = 0; i < COUNT; i++)
puts(strptrs[i]);
puts("");
print_menu();
}
puts("Bye");
return 0;
}
void sort_ASCII(char *strings[], int n)
{
// sort array of string pointers by ASCII collating sequence
char *tmp;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
{
if (strcmp(strings[i], strings[j]) > 0)
{
tmp = strings[i];
strings[i] = strings[j];
strings[j] = tmp;
}
}
}
void sort_length(char *strings[], int n)
{
// sort array of string pointers by length
char *tmp;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
{
if (strlen(strings[i]) > strlen(strings[j]))
{
tmp = strings[i];
strings[i] = strings[j];
strings[j] = tmp;
}
}
}
int fwlen(char *string)
{
// return length of first word of string
int length = 0;
// skip leading whitespace
while (isspace(*string))
string++;
// count first word length
while(!isspace(*string))
{
length++;
string++;
}
return length;
}
void sort_firstword_length(char *strings[], int n)
{
// sort array of string pointers by ASCII collating sequence
char *tmp;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
{
if (fwlen(strings[i]) > fwlen(strings[j]))
{
tmp = strings[i];
strings[i] = strings[j];
strings[j] = tmp;
}
}
}
char * get(char *string, int n)
{
// wrapper for fgets that replaces first newline with null
char *return_value = fgets(string, n, stdin);
while (*string != '\0')
{
if (*string == '\n')
{
*string = '\0';
break;
}
string++;
}
return return_value;
}
void print_menu(void)
{
puts("Choose an option:");
puts("(o) Print strings in original order.");
puts("(a) Print strings in ASCII collating sequence.");
puts("(l) Print strings ordered by length.");
puts("(f) Print strings ordered by length of the first word.");
puts("(q) Quit.");
puts("");
puts("Enter a character: ");
}