Archived

This topic is now archived and is closed to further replies.

ClearQuotes()

This topic is 6357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive been trying to write a function that accepts an array of chars and takes the first and last character off. So if filename contains: "c:\hi.txt" after this: ClearQuotes(filename); it would then contain c:\hi.txt Im just not good at manipulating string. Could someone help with this little problem, thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
taking the last character off is simple, just set the last character to 0 (the NUL character is the end-of-string marker in C/C++)
Stripping the first character comes down to incrementing the string-pointer (which is really bad, dont do this!!) or moving every character one step towards the beginning, in effect "pushing" the first character out of the string...
(Perhaps you should test to see that it is actually quotation marks you are removing)...

    
char * stripQuotes(char *str)
{
int len = strlen(str);
// Remove first char, moving the whole string:
for (int i = 0; i < len-1; ++i)
{
str<i> = str[i+1];
};

// remove last char:

str[len-2] = 0;
return str;
};


I wrote this straight out of my head now, so it probably won''t work , but you get the idea...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why do my square brackets in the expression str[ i ] turn into angle brackets > and < ??

it should be:
str[ i ] = str[ i + 1 ];

Share this post


Link to post
Share on other sites
Since the correct answer isn''t in here, I''ll put it in myself..

The sscanf solution is:
    
sscanf(in, "\"%[^\"]", out);


That is, scan and read a quote (\") then read a string (%[^\"]) that has anything except a quote.

// CHRIS

Share this post


Link to post
Share on other sites