Help with generic function to remove remove user defined characters from a string


I am having trouble developing a function that passes in a string by reference, and  a character, and then deletes all the characters from the string.

Something like:


so if word2="I" "DONT"" LIKE" "SPEECH" MARKS"


I hope you can help

Who is Participating?
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> runway_file>>word1;
>>>> if (word1=="DR")

If that compiles you don't have a char array but a string class object probably std::string.

Then the most efficient method to erasing single chars is

    size_t len = word1.size();
    char * p = &word1[0];
    char * n = &word1[0];

    for (;*p;p++)
       if (*p != '\') *n++ = *p;
       else           --len;

The above loop checks each char for not being an apostrophe and if so put it at the current position. if it is an apostrophe the current psoition was not incremented. Finally, the string was resized to the new length.

A better readable but slowervariante for the above  is

    size_t pos = 0;
    while(std::string::npos != (pos = word1.find('\'', pos))) str.erase(pos, 1);

evilrixSenior Software Engineer (Avast)Commented:
simondopickupAuthor Commented:
Thanks, they have helped and I have written something that works using string::erase.

However, how do i pass in the '  character into a function? I would pass in an inverted comma using the ' " ' notation - but cant pass in the ' character. Whose name has escaped me... :S

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

evilrixSenior Software Engineer (Avast)Commented:
Single quote :)

Just escape it thus:  '\''
>>>> However, how do i pass in the '  character into a function?

You need to add \ before, e. g.

    replacechar(str, '\'', '/');  // the middle argument is  '  \  '  '  without the spaces

Same applies for " if it one of the characters in a string or char array

   string str="want to pass double quotes \"\" within a string";

The \ is the so-called escape character. It doesn't count but only indicates that the next char isn't a special char. Hence, to pass a backslash within a literal you need two backslashes \\

   string path = "C:\\temp";
I was too slow and too chatty ...
simondopickupAuthor Commented:
Hmmm...another problem - but related to trhe question....

When i extract a string from my log - i am extracting the character string 'C'.

The aim of this thread was to create a function that would delete the first and second single quotes.

Unexpectedly, the whole string 'C' is occupying a single character space when extracted and thus I can not delete the first and second single quotations. Does anyone now why a simple string extraction using istringstream object would do this?

Sorry - will award points in a min :)
simondopickupAuthor Commented:
This is the ifstream extraction...
if (word1=="DR")
This is the line of the input file
 DR            'C'

I want the direction="C" - but the 'C' is occupying 1 character space...??
>>>> - i am extracting the character string 'C'.
That isn't a string it is a single char.

  char str[] = ;

A character sequence or array of char often is called a string.

Removing single chars from a char array means to shift left from position after the char to remove including the terminating zero char.

    int len = strlen(str) ;
    for (int i = 0; i < len; ++i)
          str[i] = str[i+1];
    len--;   // correct length for further use

Note, a char array is 0-based. By the above we copied the terminating zero char which is at pos str[len].

The str now has the contents:  "that is within single quotes\'"

To remove the final ' you could do

    str[--len] = '\0';

what first decrements the len variable and then sets a terminating zero behind the 's' of quotes.

evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
>> A better readable but slowervariante for the above  is
remove() is more readable, no?

remove(s.begin(), s.end(), '\'');
simondopickupAuthor Commented:
Sorry - hard to split these points. itsmeandnobodyelse did contribute a couple of methods that I have added to my arsenal. Thank you both
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.