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

user3512178
  • 243
  • 1
  • 2
  • 6
  • 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. – WhozCraig Jul 07 '16 at 07:09
  • It means if val is pointer type then it will be read only.Also replacing char by passing address is not possible? – user3512178 Jul 07 '16 at 07: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. – WhozCraig Jul 07 '16 at 07:24
  • 1
    Finally found the question that describes the root of your crash: [See **here**](https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha). – WhozCraig Jul 07 '16 at 07:30
  • Other than some stream helpers to make output easy, this is really a C question. – Donnie Jul 08 '16 at 05:21

2 Answers2

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;
}
Community
  • 1
  • 1
kid
  • 112
  • 1
  • 2
  • 5
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;
            }
        }
    }
}
Mikhail
  • 17
  • 3