• C

Removing double quotes from a string in the c language

I need to write a function that will remove leading and trailing quotation marks from a string in the C programming language.

So,

"token"

would convert to:

token

Thanks for your help.

Tom
tpigielskiAsked:
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.

khan_webguruCommented:
use like this

 
sep = strpbrk(line, "\\<=->/'\"");

Open in new window

0
khan_webguruCommented:
0
Anil GolamariCommented:
string newstring= oldstring.Replace("“", "").Replace("”", "");

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tpigielskiAuthor Commented:
Here are some code fragments from my program:

char *token, *newtoken;

newtoken = strpbrk(token, "\"");

I get a compile error at the strpbrk line:
operands of = have illegal types 'pointer to char' and 'int'

I don't see what is wrong

Thanks Tom
0
HooKooDooKuCommented:
I have found it very handy to write a function I usually call "TrimAndRemoveQuotes".  The concept can be implemented in any language, but the idea is to remove white spaces from the beginning and ending of a string, then look to see if the 1st and last character of the string are matching quotes (single or double) and remove them.

A possible C implementation might look something like this...


void TrimAndRemoveQuotes( char* pStr )
{
  int nLen = strlen( pStr );
  
  //Find 1st non-whitespace (currently only looks for spaces)
  int nFirst = 0;
  while( pStr[nFirst] == ' ' )
    nFirst++;	
  
  //Find Last non-whitespace (currently only looks for spaces
  int nLast = nLen - 1;
  if( nLast >= 0 )
  {
    while( pStr[nLast] == ' ' )
    {
      nLast--;
      if( nLast < 0 )
        break;
    }
  }
  
  //Test for Blank (or all white-space) string
  if( nLast < 0 )
  {
    *pStr = 0;
    return;
  }
  
  //Check for matching quotes
  if( nFirst < nLast )
  {
    if( pStr[nFirst] == pStr[nLast] )
    {
       char ch = pStr[nFirst];
       if( ch == '\'' || ch == '\"' )
       {
         nFirst++; 
         nLast--;
       }
    }
  }
  
  //Shift String in place
  char* p = pStr;
  for( int i = nFirst; i <= nLast; i++ )
  {
    *p = pStr[nFirst];
    p++;
  }
  
  //Tack on EOS
  *p = 0;
}

Open in new window

0
SuperdaveCommented:
scanf would be one easy way.

Or test the first char for quote, find the end char using strlen and test the last char for quote; if so just strcpy it.

The strpbrk error is because you're not including whatever header strpbrk is declared in (string.h), but that function will return a pointer to the first quote character so I'm not sure it's what you want.
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
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
C

From novice to tech pro — start learning today.

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.