?
Solved

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

Posted on 2009-07-14
11
Medium Priority
?
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1200 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 800 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

743 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