?
Solved

remove quotes from a char*

Posted on 2011-05-09
14
Medium Priority
?
468 Views
Last Modified: 2012-08-14
Hi

I have a char* which has quotes in it like

cons char* adlist = \" iphone, windows \";

my goal is to tokenize by commas and extract  iphone and windows  nand compare them indivually.

my code looks liem following

bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}

the problem is , when the string is tokenized the quotes are copied as well

like "iphone and
windows"

which i want to compare with teh value of client os which is iphone or windows

how do i best remove teh quotes?

i can use

 adlist.erase(remove(adlist.begin(), adlist.end(), '\"' ),adlist.end());

but that would need conversion of const ckar* to a new string ...whats the most efficient way?





bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window

0
Comment
Question by:Vlearns
  • 10
  • 4
14 Comments
 

Author Comment

by:Vlearns
ID: 35723886
      adlist++;
       adlist[strlen(adlist) - 1] = '\0';

is this the best solution?
0
 
LVL 86

Expert Comment

by:jkr
ID: 35723908
Well, you have 'no_ad_partner' as a std::string, why not removing the quotes there? E.g.
bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {

               no_ad_partner.erase(remove(no_ad_partner.begin(), no_ad_partner.end(), '\"' ),no_ad_partner.end());

               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window


Alternatively, you could remove them from 'temp':
bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       temp.str().erase(remove(temp.str().begin(), temp.str().end(), '\"' ),temp.str().end());

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window

0
 

Author Comment

by:Vlearns
ID: 35723996
if there are no quotes then the operation would still be safe , right?
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.

 

Author Comment

by:Vlearns
ID: 35724028
one more question, say

adlist was not a comma separated string but just a single token...like ...."iphone" or iphone...

this code would still work right?
0
 

Author Comment

by:Vlearns
ID: 35724047
somethings not quite right...

i am seeing

unexpected std::exception (basic_string::_S_create); re-throw...


in the logs and the code is exiting

0
 

Author Comment

by:Vlearns
ID: 35724055
pasting my code...is there anything wrong?
bool process_adlist(char* adlist, yutString cos)
{

   if (adlist && cos) {
       stringstream temp(adlist);
       temp.str().erase(remove(temp.str().begin(), temp.str().end(), '\"' ),temp.str().end());
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 35724087
Sure - yet I just noticed that you cannot use 'erase()' the way you described it, so that should be
void remove_quotes(string& s) {

  int pos = 0;

  while(string::npos != (npos = s.find('\"',pos)) s.erase(pos);
}

bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {

               remove_quotes(no_ad_partner;)

               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window


and/or
void remove_quotes(string& s) {

  int pos = 0;

  while(string::npos != (npos = s.find('\"',pos)) s.erase(pos);
}

bool  process_adlist(const char* adlist, yutString client_os)
{
   yutString cos = client_os;

   if (adlist) {
       stringstream temp(adlist);
       string no_ad_partner;

       remove_quotes(temp.str());

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false; 
}

Open in new window

0
 

Author Comment

by:Vlearns
ID: 35724126
thanks!

is there a way to do this without having to write a new function?
0
 

Author Comment

by:Vlearns
ID: 35724248
tried bool UserIMAP::process_adlist(char* adlist, yutString cos)
{
   int pos = 0;
   if (adlist && cos.c_str()) {
       stringstream temp(adlist);
       while(string::npos != (npos = temp.str().find('\"',pos))) temp.str().erase(pos);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}    

getting  error: `npos' was not declared in this scope
warning: unused variable 'npos'

0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 35724358
Srry, that was a typo on my side, it should be
void remove_quotes(string& s) {

  int pos = 0;

  while(string::npos != (pos = s.find('\"',pos)) s.erase(pos);
}

Open in new window


or
bool UserIMAP::process_adlist(char* adlist, yutString cos)
{
   int pos = 0;
   if (adlist && cos.c_str()) {
       stringstream temp(adlist);
       while(string::npos != (pos = temp.str().find('\"',pos))) temp.str().erase(pos);
       string no_ad_partner;

       while (getline(temp,no_ad_partner,',') ) {
               ia_log(L_DEBUG,"no ad partner is %s client os is %s",
                        no_ad_partner.c_str(), cos.c_str());
               if (!no_ad_partner.compare(cos.c_str())) return true;
       }
   }
   return false;
}

Open in new window

0
 

Author Comment

by:Vlearns
ID: 35724390
i am geting a compiler error

 warning: comparison between signed and unsigned integer expressions

0
 

Author Comment

by:Vlearns
ID: 35724399
changed to
uint pos = 0;
0
 
LVL 86

Expert Comment

by:jkr
ID: 35724433
Well, I should probably not post code while being on the phone ;o)

I hope it works now...
0
 

Author Closing Comment

by:Vlearns
ID: 35724484
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

862 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