Replace specific character in a string in C++ -
i have code following -
value = "current &ht"; //this value void stringset(const char * value) { const char *chk = null; chk = strpbrk(value,"&"); if(chk != null) { strncpy(const_cast<char *> (chk),"&",4) } }
in above code replace "&" value "&.it works fine if have "&" single character in current case strpbrk() return "&ht"and in below strncpy whole "&ht"is replaced.
now know methods can replace single character string.
i think need temp array hold string past & , replace & in original string , append temp array original. here above code modified, believe can use strstr instead of strchr accepts char* second argument.
void stringset(char * value) { char *chk = null,*ptr = null; chk = strchr(value,'&'); if(chk != null) { ptr = chk + 1; char* p = (char*)malloc(sizeof(char) * strlen(ptr)); strcpy(p,ptr); value[chk-value] = '\0'; strcat(value,"&"); strcat(value,p); free(p); } }
thanks niraj rathi
Comments
Post a Comment