0

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] = '!'?

5
  • 3
    Perhaps not trying to modify a read-only string literal may help. char val[] = .... And I see little sense in passing val by address to this function, btw, a problem you will need to fix if val is an array-type rather than the pointer it currently is. Commented Jul 7, 2016 at 7:09
  • It means if val is pointer type then it will be read only.Also replacing char by passing address is not possible? Commented Jul 7, 2016 at 7:21
  • The type isn't the problem; it is what it points to and what you're trying to do to that data where the wheels fall off the wagon. Your code declares a pointer that points to a read-only literal. The line you marked tries to write to that memory, and thus your program (fortunately) crashed. Commented Jul 7, 2016 at 7:24
  • 1
    Finally found the question that describes the root of your crash: See here. Commented Jul 7, 2016 at 7:30
  • Other than some stream helpers to make output easy, this is really a C question. Commented Jul 8, 2016 at 5:21

2 Answers 2

1

Check this difference between char s[] and char *s in C

#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] = '!';
                }
            }
        }
    }

}


int main() {

    char val[] = "this is string*replace next * with ! and print";
    replaceChar(val);
    cout<<"val is "<< val;
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is no need to pass pointer to pointer in method. Instead you can just pass original pointer to string. You can do it in much simpler way.

void replaceChar(char *inp){
    int i;
    int second = 0;
    /* Strings in C\C++ is null-terminated so we use it to determine 
    end of string */
    for (i = 0; inp[i] != '\0'; ++i) {
        if (inp[i] == '*') {
            /* Use flag to determine second occurrence of * */
            if (!second) {
                second = 1;
            } else {
                inp[i] = '!';
                break;
            }
        }
    }
}

1 Comment

Although correct, this does not address the root of the problem and will also crash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.