While C provides built-in string functions, you can also create custom string functions tailored to your specific needs. Custom string functions can be designed to perform tasks such as counting characters, searching for substrings, or converting case. This documentation explores the implementation of some custom string functions in C through examples.
The countChars()
function counts the occurrences of a specific character in a given string. It takes two arguments: the string and the character to count. Here's an example implementation:
#include <stdio.h>
int countChars(const char *str, char ch) {
int count = 0;
while (*str != '\0') {
if (*str == ch) {
count++;
}
str++;
}
return count;
}
int main() {
char message[] = "Hello, world!";
char character = 'l';
int count = countChars(message, character);
printf("Count of '%c': %d\n", character, count);
return 0;
}
Count of 'l': 3
countChars()
function is implemented to count the occurrences of a character in a string.const char *str
) and the character to count (char ch
) as arguments.count
is initialized to 0.while
loop, the function iterates through each character of the string until it reaches the null character ('\0'
).main()
function.The findSubstring()
function searches for a substring within a given string. It takes two arguments: the string and the substring to search for. Here's an example implementation:
#include <stdio.h>
int findSubstring(const char *str, const char *substr) {
int i, j;
for (i = 0; str[i] != '\0'; i++) {
j = 0;
while (str[i + j] == substr[j]) {
if (substr[j] == '\0') {
return i;
}
j++;
}
}
return -1;
}
int main() {
char message[] = "Hello, world!";
char substring[] = "world";
int index = findSubstring(message, substring);
if (index != -1) {
printf("Substring '%s' found at index %d\n", substring, index);
} else {
printf("Substring '%s' not found\n", substring);
}
return 0;
}
Substring 'world' found at index 7
findSubstring()
function is implemented to search for a substring within a string.const char *str
and const char *substr
) as arguments.i
and j
are declared.for
loop and a while
loop, the function compares each character of the string with the substring.main()
function.The convertCase()
function converts the case of letters in a given string. It takes a string as an argument and modifies it in-place. Here's an example implementation:
#include <stdio.h>
void convertCase(char *str) {
while (*str != '\0') {
if (*str >= 'a' && *str <= 'z') {
*str = *str - 32; // Convert to uppercase
} else if (*str >= 'A' && *str <= 'Z') {
*str = *str + 32; // Convert to lowercase
}
str++;
}
}
int main() {
char message[] = "Hello, World!";
convertCase(message);
printf("Converted: %s\n", message);
return 0;
}
Converted: hELLO, wORLD!
convertCase()
function is implemented to convert the case of letters in a string.char *str
) as an argument.while
loop, the function iterates through each character of the string until it reaches the null character ('\0'
).main()
function.By creating custom functions, you can tailor string manipulation operations to suit your specific requirements. Examples of counting characters, searching for substrings, and converting case were provided to demonstrate the implementation of these custom functions. Custom string functions can enhance the functionality of your C programs and make string manipulation tasks more efficient and convenient.