-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA12.c
More file actions
288 lines (248 loc) · 6.6 KB
/
Copy pathDSA12.c
File metadata and controls
288 lines (248 loc) · 6.6 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
This code base own and maintained by Tanmoy Samnata using standardization C23 (ISO/IEC 9899:2024)
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Find the highest frequency character in a string.
int highestFrequencyCharacter()
{
char inputString[] = "welcome to c programming in 2026!";
int frequencyCount[256] = {0}, maxFreq = 0, i = 0;
char maxChar = '\0';
while (inputString[i])
frequencyCount[(unsigned char)inputString[i++]]++;
for (i = 0; i < 256; i++)
{
if (i != ' ' && frequencyCount[i] > maxFreq)
{
maxFreq = frequencyCount[i];
maxChar = (char)i;
}
}
if (maxFreq > 0)
printf("The highest frequency character is '%c' appearing %d times.\n", maxChar, maxFreq);
else
printf("No valid characters found.\n");
return 0;
}
// Find the lowest frequency character in a string.
int findLowestFrequencyCharacter(const char *inputString)
{
if (!inputString || !*inputString)
return -1; // Handle NULL or empty string
int freq[256] = {0}, len = strlen(inputString), minFreq = len + 1;
char minChar = '\0';
for (int i = 0; i < len; i++)
freq[(unsigned char)inputString[i]]++;
for (int i = 0; i < 256; i++)
{
if (freq[i] > 0 && !isspace(i) && freq[i] < minFreq)
{
minFreq = freq[i];
minChar = (char)i;
}
}
if (minChar)
printf("The lowest frequency character is '%c' appearing %d time(s).\n", minChar, minFreq);
else
printf("No valid characters found.\n");
return 0;
}
// Count the frequency of each character in a string.
int countCharacterFrequencies(const char *inputString)
{
if (!inputString || !*inputString)
return -1;
int freq[256] = {0};
for (int i = 0; inputString[i] != '\0'; i++)
freq[(unsigned char)inputString[i]]++;
printf("Character Frequencies:\n");
for (int i = 0; inputString[i] != '\0'; i++)
{
unsigned char c = (unsigned char)inputString[i];
if (freq[c] > 0)
{
printf("'%c' : %d time(s)\n", c, freq[c]);
freq[c] = 0;
}
}
return 0;
}
// Remove the last occurrence of a character from a string.
void removeLastOccurrence(char *str, char target)
{
int lastIdx = -1;
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] == target)
lastIdx = i;
}
if (lastIdx != -1)
{
// Shift characters left to overwrite the target
for (int i = lastIdx; str[i] != '\0'; i++)
{
str[i] = str[i + 1];
}
}
}
int RemoveLastCharacter()
{
char myString[] = "pie";
removeLastOccurrence(myString, 'p');
printf("Result: %s\n", myString);
return 0;
}
void removeAll(char str[], char target)
{
int write = 0;
for (int read = 0; str[read] != '\0'; read++)
{
if (str[read] != target)
{
str[write++] = str[read];
}
}
str[write] = '\0';
}
int RemoveAllOccurrences()
{
char inputString[] = "pie";
removeAll(inputString, 'p');
printf("Resulting String: %s\n", inputString);
return 0;
}
// Delete all occurrences of a character from a string.
int DeleteAllOccurrences()
{
char inputString[] = "pie";
removeAll(inputString, 'p');
printf("Resulting String: %s\n", inputString);
return 0;
}
// Remove all repeated characters from a given string.
void deduplicate(char str[])
{
int seen[256] = {0};
int write = 0;
for (int read = 0; str[read] != '\0'; read++)
{
unsigned char c = (unsigned char)str[read];
// Check if the flag is 0 (not seen)
if (!seen[c])
{
seen[c] = 1;
str[write++] = str[read];
}
}
str[write] = '\0';
}
int RemoveRepeatedCharacters()
{
char inputString[] = "programming in 2026";
deduplicate(inputString);
printf("Resulting String: %s\n", inputString);
return 0;
}
// Replace the first occurrence of a character with another in a string.
// Returns 1 if replacement occurred, 0 otherwise
int replaceFirst(char str[], char target, char replacement)
{
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] == target)
{
str[i] = replacement;
return 1;
}
}
return 0;
}
int ReplaceFirstOccurrence()
{
char inputString[] = "pie";
if (replaceFirst(inputString, 'p', 'x'))
printf("Resulting String: %s\n", inputString);
else
printf("Character not found.\n");
return 0;
}
// Replace the last occurrence of a character with another in a string.
int replaceLast(char str[], char target, char replacement)
{
int lastFoundIndex = -1;
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] == target)
{
lastFoundIndex = i;
}
}
if (lastFoundIndex != -1)
{
str[lastFoundIndex] = replacement;
}
return lastFoundIndex;
}
int ReplaceLastOccurrence()
{
char inputString[] = "pie";
if (replaceLast(inputString, 'p', 'x') != -1)
printf("Resulting String: %s\n", inputString);
else
printf("Character not found.\n");
return 0;
}
// Put all occurrences of a character with another in a string.
void replaceAll(char str[], char target, char replacement)
{
for (int i = 0; str[i] != '\0'; i++)
{
if (str[i] == target)
{
str[i] = replacement;
}
}
}
int ReplaceAllOccurrences()
{
char inputString[] = "pie";
replaceAll(inputString, 'p', 'x');
printf("Resulting String: %s\n", inputString);
return 0;
}
int FindWord()
{
char inputString[] = "Welcome to C programming in 2026!";
char targetWord[] = "programming";
// strstr returns a pointer to the first occurrence
char *ptr = strstr(inputString, targetWord);
if (ptr != NULL)
{
// Calculate the index by subtracting the pointer to the start
int foundIndex = ptr - inputString;
printf("The word \"%s\" was first found at index: %d\n", targetWord, foundIndex);
}
else
{
printf("The word \"%s\" was not found.\n", targetWord);
}
return 0;
}
int main()
{
// Call the functions to demonstrate their functionality
highestFrequencyCharacter();
findLowestFrequencyCharacter("welcome to c programming in 2026!");
countCharacterFrequencies("welcome to c programming in 2026!");
RemoveLastCharacter();
RemoveAllOccurrences();
DeleteAllOccurrences();
RemoveRepeatedCharacters();
ReplaceFirstOccurrence();
ReplaceLastOccurrence();
ReplaceAllOccurrences();
FindWord();
return 0;
}