|
int main() {
|
|
printf("=== String Manipulation Tests ===\n\n");
|
|
|
|
char *text = "Hello World";
|
|
|
|
printf("Testing strpos:\n");
|
|
int pos = strpos(text, "World");
|
|
printf("Position of 'World' in 'Hello World': %d\n", pos);
|
|
int pos2 = strpos(text, "xyz");
|
|
printf("Position of 'xyz' in 'Hello World': %d\n\n", pos2);
|
|
|
|
printf("Testing substr:\n");
|
|
char *sub = substr(text, 0, 5);
|
|
printf("substr('Hello World', 0, 5) = '%s'\n", sub);
|
|
char *sub2 = substr(text, 6, 5);
|
|
printf("substr('Hello World', 6, 5) = '%s'\n\n", sub2);
|
|
|
|
printf("Testing upper and lower:\n");
|
|
char *up = upper(text);
|
|
printf("upper('Hello World') = '%s'\n", up);
|
|
char *low = lower(text);
|
|
printf("lower('Hello World') = '%s'\n\n", low);
|
|
|
|
printf("Testing strip:\n");
|
|
char *spaced = " Hello World ";
|
|
char *stripped = strip(spaced);
|
|
printf("strip(' Hello World ') = '%s'\n\n", stripped);
|
|
|
|
printf("Testing replace:\n");
|
|
char *replaced = replace(text, "World", "Python");
|
|
printf("replace('Hello World', 'World', 'Python') = '%s'\n", replaced);
|
|
char *replaced2 = replace("aaa bbb aaa", "aaa", "xxx");
|
|
printf("replace('aaa bbb aaa', 'aaa', 'xxx') = '%s'\n\n", replaced2);
|
|
|
|
printf("Testing startswith:\n");
|
|
if (startswith(text, "Hello")) {
|
|
printf("'Hello World' starts with 'Hello': true\n");
|
|
}
|
|
if (startswith(text, "World")) {
|
|
printf("'Hello World' starts with 'World': true\n");
|
|
} else {
|
|
printf("'Hello World' starts with 'World': false\n");
|
|
}
|
|
|
|
printf("\nTesting endswith:\n");
|
|
if (endswith(text, "World")) {
|
|
printf("'Hello World' ends with 'World': true\n");
|
|
}
|
|
if (endswith(text, "Hello")) {
|
|
printf("'Hello World' ends with 'Hello': true\n");
|
|
} else {
|
|
printf("'Hello World' ends with 'Hello': false\n");
|
|
}
|
|
|
|
printf("\nTesting slicing [start:end]:\n");
|
|
char *str = "Python Programming";
|
|
char *slice1 = str[0:6];
|
|
printf("'Python Programming'[0:6] = '%s'\n", slice1);
|
|
char *slice2 = str[7:18];
|
|
printf("'Python Programming'[7:18] = '%s'\n", slice2);
|
|
char *slice3 = str[7:11];
|
|
printf("'Python Programming'[7:11] = '%s'\n", slice3);
|
|
|
|
printf("\nCombining operations:\n");
|
|
char *combined = upper(str[0:6]);
|
|
printf("upper('Python Programming'[0:6]) = '%s'\n", combined);
|
|
|
|
char *chain = replace(lower("HELLO WORLD"), "world", "python");
|
|
printf("replace(lower('HELLO WORLD'), 'world', 'python') = '%s'\n", chain);
|
|
|
|
return 0;
|
|
}
|