-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog1.c
60 lines (52 loc) · 1.13 KB
/
prog1.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define BUFFSIZE 1024
//a more "laid back" approach for the qsort() comparison function:
int cmp(const void *p1, const void *p2)
{
char *s1 = *(char **)p1;
char *s2 = *(char **)p2;
return(strcmp(s1, s2));
}
/*
//the version that most experienced C programmers would write
int cmp2(const void *p1, const void *p2)
{
return *((int *)p1) - *((int *)p2);
}
*/
int main(int argc, char *argv[])
{
char **strings = NULL;
//int something[5];
int i, n, len;
//char a = 64;
//char b = 'A'; // == 64
char buf[BUFFSIZE +1];
printf("Enter # of Strings:");
fflush(stdout);
fgets(buf, BUFFSIZE, stdin); //or could use scanf()
n = atoi(buf);
strings = malloc(n*sizeof(char *));
for (i = 0; i < n; i++)
{
printf("Enter string #%d: ", i+1);
fgets(buf, BUFFSIZE-1, stdin);
len = strlen(buf);
if (buf[len-1] == '\n')
{
buf[len-1] = 0;
}
strings[i] = malloc((strlen(buf)+1) *sizeof(char));
strcpy(strings[i], buf);
}
qsort(strings, n, sizeof(char*), cmp);
printf("Sorted strings:\n");
for (i = 0; i < n; i++)
{
printf("%s\n", strings[i]);
}
free(strings);
return 0;
}