| | |
replace a substring by a new substring.
need to replace all occurences if substring by new string. below code replace first occurence but not other substrings present in input string.
fnReplaceString() { char acInpString[MAX_LENGTH],acOrig[MAX_LENGTH],acReplace[MAX_LENGTH]; char *pcInpStringPtr = acInpString; char *pcOrigPtr = acOrig; char *pcReplacePtr = acReplace; char *pPos; int iRes = 0; int iCount = 0; fflush(stdin); system("clear"); /* gets the input string from user */ GET_STRING(acInpString); printf("\n Enter substring to be replaced\n"); gets(acOrig); printf("\n Enter Substring to be placed \n"); gets(acReplace); /* check if substring to be replaced is present in entered string by user */ if(!(pPos=strstr(acInpString,acOrig))) { printf("\n SUBSTRING TO BE REPLACED NOT PRESENT IN GIVEN STRING \n"); } else { char acTemp[MAX_LENGTH]; strncpy(acTemp,pcInpStringPtr,pPos-pcInpStringPtr); sprintf(acTemp+(pPos-pcInpStringPtr), "%s%s", pcReplacePtr, pPos+strlen(pcOrigPtr)); puts(acTemp); } }
Similar Threads
- Find and replace string function (C++)
- string and substring (C)
- To split or not to split? To substring or not to substring? (C#)
- Getting a substring substring, but without 2 strings (C++)
- Update to completly lost : ) (Java)
- How to replace repeated substring in other string? (Java)
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi



