-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringlib.c
More file actions
174 lines (140 loc) · 2.75 KB
/
stringlib.c
File metadata and controls
174 lines (140 loc) · 2.75 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <ctype.h>
#include "stringlib.h"
int wordCount(const char* string)
{
int count = 0;
int inWord = 0;
while (*string)
{
if (!isspace((unsigned char)*string)) {
if (!inWord) {
count++;
inWord = 1;
}
} else {
inWord = 0;
}
string++;
}
return count;
}
int countVowelsInTheString(const char* string)
{
int count = 0;
while(*string)
{
char c = tolower(*string);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
string++;
}
return count;
}
/* caller must call free() */
char* reverseString(const char* string)
{
if(string == NULL) return NULL;
int stringLength = strlen(string);
char* reversed = malloc (stringLength+1);
reversed[stringLength] = '\0';
while(stringLength > 0 && *string)
{
reversed[stringLength-1] = *string;
stringLength--;
string++;
}
return reversed;
}
bool isPalindrome(const char* string)
{
bool result = false;
char* reverse = reverseString(string);
if(reverse == NULL)
return result;
result = (strcmp(reverse, string) == 0);
free(reverse);
return result;
}
int countSpace(const char* string)
{
int count = 0;
while(*string)
{
if(isspace((unsigned char)*string))
count++;
string++;
}
return count;
}
/* returns 0 based index value */
static int findCharacterIndex(const char* string, char c, bool isFirstIndex)
{
int index = -1;
if(string == NULL)
return -1;
for(int i=0; string[i] != '\0'; i++)
{
if(string[i] == c)
{
if(isFirstIndex)
return i;
index = i;
}
}
return index;
}
int findFirstCharacterIndex(const char* string, char c)
{
return findCharacterIndex(string, c, true);
}
int findLastCharacterIndex(const char* string, const char c)
{
return findCharacterIndex(string, c, false);
}
void convertToLowerCase(char* string)
{
if(!string) return;
while(*string)
{
*string = tolower(*string);
string++;
}
}
void convertToUpperCase(char* string)
{
if(!string) return;
while(*string)
{
*string = toupper(*string);
string++;
}
}
static int countCharacter(const char* string, char c, bool isIgnoreCaseSensitive)
{
int count = 0;
if(string == NULL)
return count;
while(*string)
{
if(isIgnoreCaseSensitive)
{
if(tolower((unsigned char)c) == tolower((unsigned char)*string))
count++;
}
else
{
if((unsigned char)c == (unsigned char)*string)
count++;
}
string++;
}
return count;
}
int countCharacterCaseSensitive(const char* string, char c)
{
return countCharacter(string, c, false);
}
int countCharacterIgnoreCaseSensitive(const char* string, char c)
{
return countCharacter(string, c, true);
}