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

Experts,

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:

word1=STRING_REMOVE_CHAR(word2,")

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

word1 = I DONT LIKE SPEECH MARKS

I hope you can help


simondopickupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
0
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

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

Just escape it thus:  '\''
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

itsmeandnobodyelseCommented:
>>>> 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";
0
itsmeandnobodyelseCommented:
I was too slow and too chatty ...
0
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 :)
0
simondopickupAuthor Commented:
This is the ifstream extraction...
runway_file>>word1;
if (word1=="DR")
{
      runway_file>>direction;
      STRING_REMOVE_CHAR(direction,'\'');
}
This is the line of the input file
 DR            'C'

I want the direction="C" - but the 'C' is occupying 1 character space...??
0
itsmeandnobodyelseCommented:
>>>> - 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.

   
0
itsmeandnobodyelseCommented:
>>>> 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;
    word1.resize(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);





0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
>> A better readable but slowervariante for the above  is
remove() is more readable, no?

remove(s.begin(), s.end(), '\'');
http://www.cplusplus.com/reference/algorithm/remove/
0
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.