using points to remove a char from an inputted string

using pointers, I have to function that removes a character.

You have to input a string.
Then input a char to remove.
It should tell you how many char it removed and the final string.

I have this now but I do not know how to make it input a char to remove.

This is what I have now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>

using namespace std;
void RemoveCharFromString(char * p, char c);

int main()
{
	char str[100];
	cout << "Enter a string" << endl;
	cin.getline(str, 99, '\n');

	cout << "Enter a character to remove" << endl;

	printf("%s\n", str);

	RemoveCharFromString(str, 'i');

	printf("%s\n", str);
	return 0;
}

void RemoveCharFromString(char * p, char c)
{
	if (NULL == p)
		return;
	char * pDest = p;

	while (*p)
	{
		if (*p != c)
			*pDest++ = *p;
		p++;
	}
	*pDest = '\0';
}
I do not know how to make it input a char
1
2
3
    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:

 
    cin.getline(str, sizeof(str));


See http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
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
Last edited on
Thanks. I got it to work.

I just need help on the function displaying how many characters it removed.

Like if it removed 4 i's, It displays the number 4.

1
2
3
4
5
6
7
8
9
10
11
int countChar(char str[])
{
	int count = 0;
	char *ch;

	ch = str;
	{
			count++;
	}
	return count;
}


I have this right now but it always says it only removed 1 char even though it removed more.
Last edited on
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).
Last edited on
So how would I put the count function within the removechar function?
function? Its a 1 line counter.

int remcount = 0;
if (*p != c)
{
*pDest++ = *p;

remcount++;
}
Last edited on
I am still lost.

That function does not output the removed characters.
As I suggested, you would need to (if permitted by the assignment you are attempting) change the signature of the function:
 
void RemoveCharFromString(char * p, char c)

This would become
 
int RemoveCharFromString(char * p, char c)

The function itself would keep a tally of the count of removed characters, and return that value at the end of the function.

Then it would be the task of the calling code inside main() to receive that value:
 
    int countremoved = RemoveCharFromString(str, 'i');
and output the result.
I need to start a new thread.

I am still lost.
Last edited on
How about making the changes recommended above, and compiling and running the program?


Also then post the latest version of the code.

If you get a compiler error, post it here in full and someone will help.

If you get incorrect results, state what the problem is and someone will try to help.


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;
}

Last edited on
It is still not counting the removed characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;
void RemoveCharFromString(char * p, char ch);


int main()
{
	char str[100];
	cout << "Enter a string" << endl;
	cin.getline(str, sizeof(str));

	char ch;
	cout << "Enter a character to remove:\n";
	cin >> ch;

	RemoveCharFromString(str, ch);
	removedcount = removechars(somestring, 'x');

	cout << "Removed " << removedcount << " " << ch << " characters. Your string is now:" << endl;
	printf("%s\n", str);
	return 0;
}

void RemoveCharFromString(char * p, char ch)

{
	if (NULL == p)
		return;
	char * pDest = p;

	while (*p)
	{
		if (*p != ch)
			*pDest++ = *p;
		p++;
	}
	*pDest = '\0';
}

int removechars(char *cp, char c)
{
		return numremoved;
}
Last edited on
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.
1
2
	RemoveCharFromString(str, ch);
	removedcount = removechars(somestring, 'x');
becomes:
 
	int removedcount = RemoveCharFromString(str, ch);


Next, the function RemoveCharFromString() needs to be modified to do the actual counting and to return the value.

Now here's the whole thing together. Pay attention to the use of the count variable in the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

int RemoveCharFromString(char * p, char ch);

int main()
{
    char str[100];
    cout << "Enter a string" << endl;
    cin.getline(str, sizeof(str));

    char ch;
    cout << "Enter a character to remove:\n";
    cin >> ch;

    int removedcount = RemoveCharFromString(str, ch);

    cout << "Removed " << removedcount << " " << ch 
         << " characters. Your string is now:\n" 
         << str << endl;

    return 0;
}

int RemoveCharFromString(char * p, char ch)
{
    int count = 0;
    if (NULL == p)
        return count;
        
    char * pDest = p;

    while (*p)
    {
        if (*p != ch)
            *pDest++ = *p;
        else
            count++;
        p++;
    }
    *pDest = '\0';
    return count;
}

Last edited on
Topic archived. No new replies allowed.