#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef void (*funcPtr)(char *);
// 1. Remove leading and trailing whitespace (similar to Python's strip)
void trim(char *str) {
int i = 0, j;
int len = strlen(str);
// Remove leading whitespace
while (isspace(str[i])) i++;
// Remove trailing whitespace
j = len - 1;
while (j >= 0 && isspace(str[j])) j--;
if (j < i) {
str[0] = '\0';
return;
}
memmove(str, str + i, j - i + 1);
str[j - i + 1] = '\0';
}
// 2. Remove specified punctuation (!#?)
void remove_punctuation(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (str[i] != '!' && str[i] != '#' && str[i] != '?') {
str[j++] = str[i];
}
i++;
}
str[j] = '\0';
}
// 3. Implement title case (similar to Python's title)
void title_case(char *str) {
int new_word = 1;
for (int i = 0; str[i]; i++) {
if (isalpha(str[i])) {
if (new_word) {
str[i] = toupper(str[i]);
new_word = 0;
} else {
str[i] = tolower(str[i]);
}
} else {
new_word = 1;
}
}
}
int main() {
char input[] = " !HellO ###woRld!!! "; // Test input
// Define function pointer array
funcPtr funcArray[3] = {
trim,
remove_punctuation,
title_case
};
for(int i=0;i<3;i++){
funcArray[i](input);
}
printf("Cleaned result: \"%s\"\n", input);
return 0;
}
Cleaned result: "Hello World"
Below is the Python code implementation
import re
def remove_punctuation(value):
return re.sub("[!#?]", "", value)
clean_ops = [str.strip, remove_punctuation, str.title] # Remove leading and trailing spaces, remove special characters, format camel case words
result = []
def clean_string(value):
for func in clean_ops:
value = func(value)
result.append(value)
return result
print(clean_string(" !HellO ###woRld!!! "))
['!HellO ###woRld!!!', 'HellO woRld', 'Hello World']