-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
74 lines (62 loc) · 1.45 KB
/
sort.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
#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;
char cityPop[20];
int cityCount = 0;
char *line;
size_t size;
for(int i = 0; i <= ENTRIES; i++){
usCity[i] = NULL;
}
printf("\nHit ctrl D to Quit\n");
while(1){
printf("\nEnter a city name\n");
if(getline(&line, &size, stdin) == EOF){
break;
}
else{
usCity[cityCount] = (char *)malloc(sizeof(char) * (size + 10));
strncpy(usCity[cityCount], line, size);
printf("\nEnter population (2digits)\n");
if(getline(&line, &size, stdin) == EOF){
break;
}
else if(atoi(line) > 15){
cityPop[cityCount] = atoi(line);
cityCount = cityCount + 1;
}
else{
free(usCity[cityCount]);
}
}
}
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;
}