-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintsort.c
97 lines (69 loc) · 1.68 KB
/
printsort.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
95
96
97
#include <stdio.h>
void readstring(char str[]) {
int i = 0;
char c;
// Read characters from input until newline or end of array is reached
while (i < 19 && (c = getchar()) != '\n') {
str[i] = c;
i++;
}
// Add null terminator to end of string
str[i] = '\0';
}
//count letter function to see how many times to print
int count_letters(char letter, char str[]){
int length = 0; int count = 0;
while (str[length] != '\0'){
length++; //Sets length to be the right length
}
for (int i = 0; i < length; i++){
if (letter == str[i]){
count++;
}
}
return count;
}
void sort_function(char str[]){
int x = 0; int length = 0; int i = 0; int k = 0; int count;
char lowest; int repeats = 0; char secondlowest; char used; char value;
while (str[length] != '\0'){
length++; //Sets length to be the right length
}
used = '0';
for (i = 0; i < length; i++){
lowest = '{';
for (k = 0; k < length; k++){
if ((used < str[k]) && (str[k] < lowest)){
lowest = str[k];
}
}
value = lowest;
count = count_letters(value, str);
for (int i = 0; i < count; i++){
printf("%c", value);
}
used = lowest;
}
}
int main(void){
int x = 1;
while (x == 1){
char str[20];
printf("Enter word: ");
readstring(str);
if (str[0] == '\0'){
printf("\nExiting");
printf("%c", '\n');
x = 0;
}
else
{
printf("Original word: %s\n" , str);
printf("Alphabetized word: ");
sort_function(str);
printf("%c", '\n');
x = 1;
}
}
return 0;
}