Replace a Substring Within a String in C
In C, replacing a substring within a string requires manually locating the substring and modifying the original string since C lacks built-in functions for this operation. We can achieve this using standard functions like strstr(), strcpy(), strcat(), and string manipulation techniques.
In this tutorial, we will explore multiple ways to replace a substring within a string, with examples.
Examples of Replacing a Substring in C
1. Replacing a Substring Using strstr() and String Manipulation
In this example, we locate a substring using strstr(), extract parts of the original string, and construct a new string with the replacement.
main.c
</>
Copy
#include <stdio.h>
#include <string.h>
void replaceSubstring(char *str, const char *oldWord, const char *newWord) {
char result[1000];
char *pos, temp[1000];
int index = 0;
int oldWordLen = strlen(oldWord);
// Copy original string to temp
strcpy(temp, str);
#include <stdio.h>
#include <string.h>
void replaceSubstring(char *str, const char *oldWord, const char *newWord) {
char result[1000];
char *pos, temp[1000];
int index = 0;
int oldWordLen = strlen(oldWord);
// Copy original string to temp
strcpy(temp, str);
while ((pos = strstr(temp, oldWord)) != NULL) {
// Copy characters before the oldWord
int lenBefore = pos - temp;
strncpy(result + index, temp, lenBefore);
index += lenBefore;
// Copy the newWord
strcpy(result + index, newWord);
index += strlen(newWord);
// Move past the oldWord in temp
strcpy(temp, pos + oldWordLen);
}
// Copy the remaining part of temp
strcpy(result + index, temp);
// Copy result back to original string
strcpy(str, result);
}
int main() {
char str[1000] = "Hello world, welcome to the C world!";
replaceSubstring(str, "world", "universe");
printf("%s\n", str);
return 0;
} while ((pos = strstr(temp, oldWord)) != NULL) {
// Copy characters before the oldWord
int lenBefore = pos - temp;
strncpy(result + index, temp, lenBefore);
index += lenBefore;
// Copy the newWord
strcpy(result + index, newWord);
index += strlen(newWord);
// Move past the oldWord in temp
strcpy(temp, pos + oldWordLen);
}
// Copy the remaining part of temp
strcpy(result + index, temp);
// Copy result back to original string
strcpy(str, result);
}
int main() {
char str[1000] = "Hello world, welcome to the C world!";
replaceSubstring(str, "world", "universe");
printf("%s\n", str);
return 0;
}
Explanation:
- We define a function
replaceSubstring()that replacesoldWordwithnewWordin the input string. strstr()is used to locate occurrences ofoldWordwithin the input string.- A temporary buffer
result[]stores the modified string. - The loop iterates over the input string, replacing occurrences of
oldWordwithnewWord. - The modified string is copied back to the original string.
Output:
Hello universe, welcome to the C universe!
Conclusion
In this tutorial, we explored different ways to replace a substring within a string in C:
- Using
strstr()and string manipulation: Finds and replaces substrings using built-in functions.
