#include <stdio.h>
#include <string.h>
int main() {
char input_string[] = "my name adarsh";
char search[] = "name";
char replace[] = "was";
// Find the index of the first occurrence of "name"
char *found = strstr(input_string, search);//3
printf("%c",*found);
if (found != NULL) {
// Calculate the index)
int index = found - input_string;//"name" - wholestr.. finds where name is present;
printf("%d",index);
// Create a modified string
char modified_string[100]; // Make sure it's large enough to hold the result
strncpy(modified_string, input_string, index);//3 ,untill *found it copies
modified_string[index] = '\0'; // Null-terminate the modified string
strcat(modified_string, replace);//we add new word
strcat(modified_string, found + strlen(search));//name-> after that index is been taken
//Since found already has whole input_string inside it we just make it to point after name
//so " adarsh" is been updated found value
printf("Modified string: %s\n", modified_string);
printf("Index of 'is': %d\n", index);
} else {
printf("'is' not found in the input string.\n");
}
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: