Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- char *fnReplace(char sString[], char sPoor[], char sGreat[]); // Function to replace the words
- int main()
- {
- char sString[] = "I am a poor programmer";
- printf("Input string: %s\n", sString);
- printf("Replaced substring: %s\n", fnReplace(sString, "poor", "great"));
- return 0;
- }
- char *fnReplace(char sString[], char sPoor[], char sGreat[])
- {
- int iStLen = strlen(sString); // In this variable we save the length of sString[] (without the '\0')
- int iPoorLen = strlen(sPoor); // In this variable we save the length of sPoor[] (without the '\0')
- int iGreatLen = strlen(sGreat); // In this variable we save the length of sGreat[] (without the '\0')
- char *pPointer = NULL; // We use this pointer for the loop.
- for (pPointer = sString; pPointer = strstr(pPointer, sPoor); ++pPointer)
- {
- if (sPoor != sGreat) // Here we check if the words are with different length (in this case they are) and make the array "bigger", so we can save and see whole of it.
- {
- memmove(pPointer + iGreatLen, pPointer + iPoorLen, iStLen - (pPointer - sString) + iGreatLen);
- memcpy(pPointer, sGreat, iGreatLen);
- }
- }
- return sString;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement