-
Notifications
You must be signed in to change notification settings - Fork 5
/
str_reverse.c
100 lines (82 loc) · 2.02 KB
/
str_reverse.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
98
99
100
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
char *substr(char *, size_t);
/**
* Solution 1: By swapping elements.
* Will override given string.
*
* @param char* str
* @param int index
* @param int size
*
* @return void
*/
void str_reverse_1(char *str, int index, int size)
{
if (index == size / 2) {
return;
}
char temp = str[index];
str[index] = str[size - index];
str[size - index] = temp;
str_reverse_1(str, index + 1, size);
}
/**
* Solution 2: By using temporary var.
*
* @param char* str
*
* @return char* reversed string
*/
char *str_reverse(char *str)
{
size_t len = strlen(str);
static char *reverse = "";
static int i = 0;
if (i == 0) {
reverse = (char *) malloc(len * sizeof(char));
}
if (len < 1) {
reverse[i] = '\0';
i = 0;
return reverse;
}
reverse[i++] = str[len - 1];
return str_reverse(substr(str, len));
}
/**
* Get substring of given length.
*
* @param char* str
* @param size_t len
*
* @return char*
*/
char *substr(char *str, size_t len)
{
char *temp = (char *) malloc(len * sizeof(char));
strncpy(temp, str, len - 1);
temp[len - 1] = '\0';
return temp;
}
int main()
{
assert(strcmp("a", str_reverse("a")) == 0);
assert(strcmp("tikna", str_reverse("ankit")) == 0);
assert(strcmp("lerhkop tikna", str_reverse("ankit pokhrel")) == 0);
assert(strcmp(".egaugnaL gnimmargorP C", str_reverse("C Programming Language.")) == 0);
assert(strcmp("!dlrow yzarc a si sihT", str_reverse("This is a crazy world!")) == 0);
assert(strcmp(
".tile gnicsipida rutetcesnoc ,tema tis rolod muspi meroL",
str_reverse("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
) == 0);
char s[20] = "ankit";
str_reverse_1(s, 0, (int) strlen(s) - 1);
assert(strcmp("tikna", s) == 0);
strcpy(s, "ankit pokhrel");
str_reverse_1(s, 0, (int) strlen(s) - 1);
assert(strcmp("lerhkop tikna", s) == 0);
return 0;
}