How to remove characters from char?

I would like to remove certain characters from a char.
lets say the char is "abcd" how could i remove the b character and make the char equal to "acd".
Traverse the array and then find the character you want to get rid of (either a spacific character, or a spacific place in the array or both) and then change the rest of the array using pointers.
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
#include <iostream>

using namespace std;

void popchar( char *c , int x , int size)
{
	int i;
	for ( i = x; i < size; i++)
	{
		if (i > x)
		{
			c[i-1] = c[i];
		}
	}
}

int main()
{
	// '\0' Null char needed 
	char text[] = { 'a','b','c','d','e','f','g','\0' };
	cout << text << endl;
	popchar( text , 1 , sizeof(text));
	popchar( text , 4 , sizeof(text));
	
	cout << text << endl;
	cout << sizeof(text) << endl;
	
	return 0;
}
Last edited on
You aren't really explaining anything, DrakeMagi. I could have given him/her code doing whar s/he wanted, but I think (as some people have pointed out to me in the past) that its better to explain what to do rather then just give code.

Also, you can declare text like:

char text[] = "abcdefg";

a null will be added automatically at the end of the array.
Last edited on
I learn faster by code then talk. When i looked for something. I look for code. Not what people chatter about.
Maybe you do, but many people will just copy/paste and learn nothing. Examples are all find and good, but you shouldn't be solving the problem for them, there is a reason they are assigned.
@OP: It's not a char, it's a string. A char specifies an individual letter. That's a char[] (array of chars, also known as a c-string or char*). You should have a pretty easy time doing this if you use std::string instead of c-strings.
I actually find that you learn more if you just use c-strings.

I learned WAY more about pointers than I ever did when I stopped using string.
I vaguely agree with you. But only vaguely. std::string is more reliable and safer. Knowing how to use cstrings is not a bad thing in my books, but I'd not say it's a ... major skill to worry about.
<entirely personal opinion>
I disagree. It's not hard to use C-strings properly, you just gotta remember about the NULL and that's pretty much it. Internally, a C++ string is a C string with a bunch of abstraction on top, thats' all. I prefer good ol' fashioned Cola.
Internally, a C++ string is a C string with a bunch of helpful abstraction on top, thats' all.


Fix'd
I know it's helpful, but it's still abstraction. But a C++ string is just a C string inside a class.
Topic archived. No new replies allowed.