-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31_sort_Array.c
94 lines (87 loc) · 1.47 KB
/
31_sort_Array.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
/*
* Author: Girish Gaude
* Date: 3/Jan/2020
* Desciption: Program to
* Input: Enter
* Output: Display
*/
#include<stdio.h>
#include<stdio_ext.h>
#include<stdlib.h>
int my_cmp( char *s1, char *s2);
void my_cpy(char *s1, char *s2);
void sort_name(char** s1, int row)
{
char temp[32];
for(int i=0;i<row;i++)
{
for(int j = i+1; j<row; j++)
{
if(my_cmp(s1[i],s1[j])>0)
{
my_cpy(temp,s1[i]);
my_cpy(s1[i],s1[j]);
my_cpy(s1[j],temp);
}
}
}
}
int my_cmp( char *s1, char *s2)
{
while((*s1 != '\0' && *s2 != '\0') && *s1 == *s2)
{
s1++;
s2++;
}
if(*s1 == *s2)
return 0;
else
return *s1 - *s2;
}
void my_cpy(char *s1, char *s2)
{
char temp[50] = {'\0'};
int i = 0;
while(s2[i] != '\0')
{
s1[i] = s2[i];
i++;
}
s1[i] = '\0';
}
int main()
{
char ch;
do
{
int row;
printf("Enter number of Row\n");
scanf("%d",&row);
char* arr[row];
printf("Enter the %d names of lenght max 32 character in each\n", row);
for(int k=0; k<row; k++)
{
arr[k] =(char*)malloc(row*sizeof(char[32]));
}
if (arr == NULL)
{
printf("ERROR : Memory not allocatedi\n");
}
__fpurge(stdin);
for(int i=0; i<row; i++)
{
scanf(" %32[^\n]",arr[i]);
__fpurge(stdin);
}
sort_name(arr,row);
printf("Sorted Names are: \n");
for(int j=0; j<row; j++)
{
printf(" %s\n", arr[j]);
}
printf("Do you want to continue.\n");
getchar();
scanf("%c", &ch);
}while( ch == 'y' );
return 0;
}