char ch;
cout << "Enter a character to remove:\n";
cin >> ch;
By the way, in the cin.getline(), the length should be 100, not 99, as the value is the maximum including the terminating null character. Since the default delimiter is '\n', that can be omitted, and you can simply write:
yes, copying it one character at a time works. what I would do is copy / eliminate then compare the strlen() (subtract them) to find how many you removed.
You could use ... strchr(??) or whatever the "find this character in a string" function, and copy pointer chunks with memcpy or some logic to make strcat work, but this is mostly showing off, it does not gain you anything unless your strings are massive (like entire text files, I work with 3-5 GB text files and sometimes read them into a single string). memmove will solve the problem as well, for another "show off" approach.
I personally like to use [] notation instead of pointer math. Its the same thing, just looks better to me. /shrug
To find how many characters were removed, I think there are two main possibilities.
1. Do the counting at the same time as the characters are removed, inside function RemoveCharFromString(). Then that function, instead of type void, could return an int.
2. Leave the existing function unchanged. But find and store the length of the string before calling the function. Then find the length of the string afterwards and subtract. You could use strlen() here, or if you really must, write your own version of strlen() which isn't too difficult.
The second approach seems like a clumsy workaround, leaving possibilities for errors in the surrounding code. The first approach seems better - unless it is disallowed by the instructions you are following. (Actually you could use both, use the strlen() approach to verify that the remove function is doing the counting correctly).
(Your existing function countChar() seems unlikely to be able to work, it doesn't have enough information, at best it could find the current length of the string).
To be fair, we probably could have explained that better, not realizing you were not following.
What we are saying is that as you remove them, you have 2 choices... you can count them as you remove, or at the end, you can compare the lengths. Both are solid approaches. Then you can return that value from your function. That is ok, the function is still only doing 1 thing so its good programming design, it just gives some feedback on what it did with this change.
you can make a function return a value in the y = f(x) style from math. For example
int sum(int a, int b)
{
return a+b;
}
...
x = sum(1,2); //x = 3 after this.
using that approach,
int removechars(char *cp, char c)
{
...all the stuff above
return numremoved;
}
and bam..
removedcount = removechars(somestring, 'x');
cout << "you removed " << removecount <<" copies of x << endl;
You don't need a second function. If you want one, though, you can write it.
int countremoved(char* cp1, char* cp2, char c)
{
...//code to count how many c are in cp1 and not in cp2. cp2 is redundant, you can use it to validate that they are the same string apart from the 1 letter, or not have it at all and just count the # of c in the original input -- slightly off in design as it assume the remove function works.
return count;
}
First let me apologise if anything I said previously was not very clear or seemed unhelpful. The aim is to help people by giving advice in a way which people can understand and obviously whatever I said wasn't clear or helpful enough.
Anyway, moving on. Your latest code has two separate functions,
void RemoveCharFromString(char * p, char ch)
and
int removechars(char *cp, char c)
These should be merged together so there is just a single function:
int RemoveCharFromString(char * p, char ch)
Similarly inside main() there are two separate function calls which again need to be combined into just one.