0

Hi,
I'm trying to write a function that takes in a c-string, and remove all 'T' and 't' from the string.
So here's what I have so far, but every time I try to compile it, it gives an empty character constant error.

void removeT(char* msg)
{
    char* ptr;
    // Search for 't' and delete it
    ptr = strchr(msg, 't');
    while (ptr != NULL)
    {
        cerr << "found 't' at " << ptr-msg << endl;
        *ptr = '';
        ptr=strchr(ptr+1, 't');
    }
    // Search for 'T' and delete it
    ptr = strchr(msg, 'T');
    while (ptr != NULL)
    {
        cerr << "found 'T' at " << ptr-msg << endl;
        *ptr = '';
        ptr=strchr(ptr+1, 'T');
    }
}
int main()
{
    char msg[40] = "This stock price is near a bottom!";
    removeT(msg);
    cout << msg << endl;  // Should print 'his sock price is near a boom!'
}

I tried replacing '' with "", but then it just gives me another error, saying it cannot convert 'const char' to 'char'.
I know instead of

*ptr='';

I can put

*ptr=' '; // Space between ''

but that will just a space, and print out the final msg as ' his s ock price is near a bo om!'

Is there any way to go around this? Thanks in advance!

3
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by JasonHippy
0

Yes, use string.

If not, then create a variable for the left side before t, then a variable for the right side after t. Then concate left and right to make a new c-string.

0

Using strings would be preferable. However, if this is for an assignment and you have to use c style strings; in your function you could copy the original string into a 2nd character array. Then parse through the 2nd array and copy any characters that are not 't' or 'T' back into the 1st. Once you've got to the end of the 2nd array, append a null on the end of the first array and you're done!

Cheers for now,
Jas.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.