remove char without space from string

Hello guys,

Good eveninggg!

i'm new here. (only few minitues...;))
I need help in idea and I would much appreciate it.

I need to remove a char from string that found ;
for example : string- abcdfe
the char ch is - c
and the return new string is : abdfe.

I did as follow:

void removeChar(char string2[SIZE], char ch)
{
char input= 'a'; // for example replace something
int i=0;

if(string2[i]== ch && string2[i]!='\0')
{
string2[i]= input;
}
i++;
}

and in the main : I only called to this function above and print string.

thank u :)

If you are using C++ then there is standard algorithm std::remove. It allows to remove all occurences of a given character.
I got it you're here just doing for example 'abcde' to 'abade' i.e replacing c by a. If you want to eliminate c from there you have to use loop such that when 'c' is detected (as in your code in 'if' condition) you apply a loop till the end of string such that loop copies that element(e.g. 'd') which is coming after the 'c' to the cth place and copies element after 'd' i.e. e to dth place and so on. This will definitely shrink your string. I guess i have given you the logic. Rest depends upon you. Good luck
Please give the <algorithm> of stl a try

1
2
3
4
5
6
    char strs[] = "abcdfe";
    auto end = std::remove(strs, strs + sizeof(strs), 'c');
    for(auto it = strs; it != end; ++it)
        std::cout<<*it;

    std::cout<<std::endl;


Bjarne(the father of c++) recommendation

Don't use C coding standards (even if slightly modified for C++) and don't use ten-year-old C++ coding standards (even if good for their time). C++ isn't (just) C and Standard C++ is not (just) pre-standard C++.


http://www.stroustrup.com/bs_faq2.html#coding-standard

you could consider to use std::string to replace char array too
In most of the cases, the tools offer by the standard library are far more
better than handcrafted codes.

If something you’re doing isn’t plain, legible, and type-safe, chances are you're doing it the C way.
Last edited on
Rather than removing the character, you can create a new string which does not contain the character and return that one instead.
Topic archived. No new replies allowed.