Solved

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

Posted on 2009-07-14
11
352 Views
Last Modified: 2013-12-14
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


0
Comment
Question by:simondopickup
  • 4
  • 4
  • 3
11 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24847867
0
 

Author Comment

by:simondopickup
ID: 24848136
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
 
LVL 40

Expert Comment

by:evilrix
ID: 24848162
Single quote :)

Just escape it thus:  '\''
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24848202
>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24848210
I was too slow and too chatty ...
0
 

Author Comment

by:simondopickup
ID: 24848790
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
 

Author Comment

by:simondopickup
ID: 24848840
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24849076
>>>> - 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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 300 total points
ID: 24849237
>>>> 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
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 total points
ID: 24849281
>> 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
 

Author Closing Comment

by:simondopickup
ID: 31603191
Sorry - hard to split these points. itsmeandnobodyelse did contribute a couple of methods that I have added to my arsenal. Thank you both
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question