954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

replace a substring by a new substring.

0
By arsh_arsh on Nov 9th, 2009 1:44 pm

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);

  }
}

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You