-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString_Sorter.c
74 lines (65 loc) · 1.94 KB
/
String_Sorter.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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXSTR 500
#define CLEAR_INPUT while (getchar() != '\n') /*void*/
int del_enter (char *string){
if (*(string + (strlen(string)-1)) == '\n'){
*(string + (strlen(string)-1)) = '\0';
return 1;
}
return 0;
}
void sort_up (char *string){
unsigned int i, j;
char ch_tmp;
for (i = 0; i < strlen(string); i++){
for (j = 0; j < (strlen(string) - 1) - i; j++){
if (*(string + j) > *(string + 1 + j)){
ch_tmp = *(string + j);
*(string + j) = *(string + 1 + j);
*(string + 1 + j) = ch_tmp;
}
}
}
}
void sort_down (char *string){
unsigned int i, j;
char ch_tmp;
for (i = 0; i < strlen(string); i++){
for (j = 0; j < (strlen(string) - 1) - i; j++){
if (*(string + j) < *(string + 1 + j)){
ch_tmp = *(string + j);
*(string + j) = *(string + 1 + j);
*(string + 1 + j) = ch_tmp;
}
}
}
}
int main (int argc, char *argv[]){
char *string_input;
char opt;
if ((string_input = malloc(MAXSTR*sizeof (char))) == NULL){
printf("Memory error!\n");
exit(0);
}
printf ("Insert string:\n");
fgets (string_input, MAXSTR, stdin);
del_enter (string_input);
printf ("Como deseja ordenar a string? (C)rescente ou (D)ecrescente?\n");
do{
switch (tolower(opt = fgetc(stdin))) {
case 'c': sort_up (string_input);
break;
case 'd': sort_down (string_input);
break;
default: printf("Escolha \"c\" para ordem crescente ou \"d\" para ordem decrescente!\n");
break;
}
CLEAR_INPUT;
}while (opt != 'c' && opt != 'd');
printf ("String ordenada: %s\n", string_input);
free (string_input);
return 0;
}