C Online Compiler
Example: Substring Replacement (Using snprintf) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Substring Replacement (Using snprintf) #include <stdio.h> #include <stdlib.h> // For malloc, free #include <string.h> // For strlen, strstr // Function to replace all occurrences of a substring using snprintf char* replace_substring_snprintf(const char *original, const char *find, const char *replace) { char *result; char *current_pos; // Current write position in result const char *search_pos; // Current read position in original char *match; // Pointer to the found substring size_t len_find = strlen(find); size_t len_replace = strlen(replace); size_t len_original = strlen(original); int count = 0; // Number of replacements size_t final_len; // Final calculated length of the new string // Step 1: Count occurrences of 'find' search_pos = original; while ((match = strstr(search_pos, find)) != NULL) { count++; search_pos = match + len_find; } // Step 2: Calculate the final length of the result string final_len = len_original + (len_replace - len_find) * count; // Step 3: Allocate memory for the new string (+1 for null terminator) result = (char *)malloc(final_len + 1); if (!result) { return NULL; } // Step 4: Construct the new string using snprintf current_pos = result; search_pos = original; size_t remaining_space = final_len; // Track remaining space in the buffer while ((match = strstr(search_pos, find)) != NULL) { size_t len_prefix = match - search_pos; // Copy prefix int written = snprintf(current_pos, remaining_space + 1, "%.*s", (int)len_prefix, search_pos); if (written < 0 || (size_t)written > remaining_space) { // Handle error or buffer overflow (unlikely with correct length calc) free(result); return NULL; } current_pos += written; remaining_space -= written; // Copy replacement string written = snprintf(current_pos, remaining_space + 1, "%s", replace); if (written < 0 || (size_t)written > remaining_space) { free(result); return NULL; } current_pos += written; remaining_space -= written; search_pos = match + len_find; // Move search_pos past the found substring } // Step 5: Copy the remaining part of the original string snprintf(current_pos, remaining_space + 1, "%s", search_pos); return result; } int main() { const char *str = "The quick brown fox jumps over the lazy fox."; const char *find = "fox"; const char *replace = "cat"; printf("Original string: %s\n", str); char *new_str = replace_substring_snprintf(str, find, replace); if (new_str) { printf("String after replacement: %s\n", new_str); free(new_str); } else { printf("Error during replacement or memory allocation.\n"); } const char *str_long = "One two three four five six seven eight nine ten. One two three four five six seven eight nine ten."; const char *find_short = "three"; const char *replace_long = "THIRTY-THREE"; printf("Original string long: %s\n", str_long); char *new_str_long = replace_substring_snprintf(str_long, find_short, replace_long); if (new_str_long) { printf("String long after replacement: %s\n", new_str_long); free(new_str_long); } return 0; }
Output
Clear
ADVERTISEMENTS