-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.c
92 lines (79 loc) · 1.98 KB
/
text.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
#include <stdio.h>
#include "text.h"
#include "mem.h"
/*****Reverse String*****/
char* reverse_string (char* string){
size_t len = strlen (string) - 1;
size_t check = len / 2;
unsigned int i;
char tmp_char;
for (i = 0; i <= check; i++){
tmp_char = string [i];
string [i] = string [len - i];
string [len - i] = tmp_char;
}
return string;
}
/*Insertion Up Sort*/
char* insertion_up_sort (char* string){
short unsigned int y = 0, index_min = 0, x;
size_t len = 0;
char tmp_char;
len = strlen (string);
for (x = 0; x < len; x++){
index_min = x;
for (y = x; y < len; y++){
if (string [index_min] > string [y])
index_min = y;
}
tmp_char = string [x];
string [x] = string [index_min];
string [index_min] = tmp_char;
}
return string;
}
/*Insertion Down Sort*/
char* insertion_down_sort (char* string){
short unsigned int y = 0, index_min = 0, x;
size_t len = 0;
char tmp_char;
len = strlen (string);
for (x = 0; x < len; x++){
index_min = x;
for (y = x; y < len; y++){
if (string [index_min] < string [y])
index_min = y;
}
tmp_char = string [x];
string [x] = string [index_min];
string [index_min] = tmp_char;
}
return string;
}
/*Compare strings*/
unsigned short int string_cmp (char* str_a, char* str_b){
size_t len_a = strlen (str_a), len_b = strlen (str_b);
unsigned short int i, status;
if (len_a != len_b){
status = 2;
}
else{
for (i = 0; i < len_a; i++){
if (str_a [i] != str_b [i]){
status = 1;
}
else
status = 0;
}
}
return status;
}
void testing (char* string){
printf ("The string is: %s\n", string);
string = reverse_string (string);
printf ("The reversed string is: %s\n", string);
string = insertion_up_sort (string);
printf ("The insertion up sorted string is: %s\n", string);
string = insertion_down_sort (string);
printf ("The insertion down sorted string is: %s\n", string);
}