I am very new new to cpp and trying to replace second occurrence of '*' with '!' char in a given string using following method.
#include <iostream>
#include <string.h>
using namespace std;
void replaceChar(char **inp){
char *tmp = *inp;
const char *c = "*";
char *cmark = strstr(tmp,c);
cout<< *cmark;
if(cmark != NULL && strlen(cmark) > 1){
cmark++;
if(strstr(cmark,c)){
int len = strlen(cmark);
cout<<"len"<<len;
for(int i=0;i<len;i++){
if(cmark[i] == '*'){
cout<<"i.."<<i;
cmark[i] = '!';//error point
}
}
}
}
}
int main() {
char * val = "this is string*replace next * with ! and print";
replaceChar(&val);
cout<<"val is "<< val;
return 0;
}
I am getting run time error on error point line.If I comment out this line I am getting correct index of '*' to be replaced.
Is it possible to replace '*' with '!' using cmark[i] = '!'?
char val[] = .... And I see little sense in passingvalby address to this function, btw, a problem you will need to fix ifvalis an array-type rather than the pointer it currently is.