-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopysort.c
53 lines (44 loc) · 1.02 KB
/
copysort.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
static int ENTRIES = 20;
void swap(char **a, char **b);
int main(int argc, char *argv[]){
char **usCity = (char **)malloc(ENTRIES * sizeof(char *));
int cmpVal;
int cityCount = 0;
char *line;
size_t size;
while(1){
// usCity[i] = NULL;
if(getline(&line, &size, stdin) == EOF){
break;
}
usCity[cityCount] = (char *)malloc(sizeof(char) * (size + 10));
strncpy(usCity[cityCount++], line, size);
}
printf("\n\nSuccessfully Exited. Results:\n");
for (int i = 0; i < cityCount; i++){
for (int j = i + 1; j < cityCount; j++) {
cmpVal = strcasecmp(usCity[i], usCity[j]);
if (cmpVal > 0)
swap(&usCity[i], &usCity[j]);
else if (cmpVal == 0)
if (isupper(usCity[i][0]))
swap(&usCity[i], &usCity[j]);
}
}
for(int c = 0; c < cityCount; c++){
printf("%s", usCity[c]);
free(usCity[c]);
}
exit(0);
}
void swap(char **a, char **b)
{
char *temp = *a;
*a = *b;
*b = temp;
}