Advertisement

ClearQuotes()

Started by July 16, 2000 05:27 PM
5 comments, last by Esap1 24 years, 5 months ago
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
uh, Anyone?
Advertisement
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...

Why do my square brackets in the expression str turn into angle brackets &gt; and &lt; ??<br><br>it should be:<br>str = str;<br>
Of course, if you know that the first and last characters are quotes, it would be simpler to do sscanf(in, "\"[^\"]", out);
How would you use sscanf to do it,
put it in [ source ] [ /source ]. And yeah, The first and last characters are always Quotes.
Advertisement
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
// CHRIS [win32mfc]

This topic is closed to new replies.

Advertisement