Home > OS >  How can I change the value of the input index's of the function in the function?
How can I change the value of the input index's of the function in the function?

Time:12-25

How should I do it so that all the indexes of the input array of my function are converted to the character "?" I know it should be done using the address of this array, but I have no idea the function:

void something (char s1[]) {
    for (int i = 0; i < strlen (s1); i  ) {
        cout<<i;
        s1[i] = '?';
    }
}
int main() {
    something ("The String!");
    getch();
}

CodePudding user response:

I think that what you really want is to convert every charter of your input string to "?". The main issue with your code is that "The String!" is of type char* and is stored in code section of memory, so you cannot modify its characters. However you can define it as an array instead.

void something (char s1[]) {
    for (int i = 0; i < strlen (s1); i  ) {
        cout<<i;
        s1[i] = '?';
    }
}
int main() {
    char input[] = "The String!";
    something (input);
    getch();
    return 0; // do not forget to return value from your function.
}

In general I'd recommend to do not use C-style strings, but C -style std::string (you can pass it to your function by reference to omit copying).

  • Related