I have a string that has text surrounded by Quotation Marks in it:
Example: "Virginia"
How would I edit the string to keep the text in the middle but get rid of the Quotation marks?
Thanks for any help you can offer.
Printable View
I have a string that has text surrounded by Quotation Marks in it:
Example: "Virginia"
How would I edit the string to keep the text in the middle but get rid of the Quotation marks?
Thanks for any help you can offer.
This would not work if one of the " was missing i.e crash
The following is safer.Code:#include <stdio.h>
#include <string.h>
int main()
{
char s[] = "\"Virginia\"";
printf("%s\n", s);
*strstr( strcpy(s, s+1), "\"") = 0;
printf("%s\n", s);
return 0;
}
Code:char s2[] = "\"Virginia\"", *t;
printf("%s\n", s2);
if(*s2 == '\"')
{
strcpy(s2, s2+1);
if(t = strstr( s2 , "\""))
*t = 0;
}
printf("%s\n", s2);
I ran into one problem trying it that way (and it's probably just a stupid thing on me).
What I neglected to mention is that I have an array of these strings so for example: team[1] = "Virginia"
Team is declared as team[41][40]...
What I tried was:
And I got two errors:Code:/* Remove the Quotes */
for (i=1;i<number+1;i++)
{
*strstr(strcpy(team[i][j],team[i][j+1]), "\"") = 0;
}
Argument of type "char" is incompatible with parameter of type "const char *".
The source of the error was team strings.
Is there something different I would have to do to get this to work in my situation?
Again thanks for your help.
here is another way to do that:-
Code:#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char s2[]="\"Virginia\"";
int k=0,j=0;
puts(s2);
printf("\n");
k=strlen(s2);
for(j=0;j<k;j++)
{
if(s2[j]!='"')printf("%c",s2[j]);
}
printf("\n");
return 0;
system("PAUSE");
}
I just saw the second way you tried it.
That one seems to have worked THANKS!
Code:char *s;
for (i=1;i<number+1;i++)
{
s = team[i];
*strstr( strcpy(s, s+1), "\"") = 0;
}
Ok... one last question if you've got a minute.
When I'm outputting my data now I'm using code that looks like:
Everything but the abbv (%-5s) field seems to work just fine. For some reason the abbv field is producing its output and then appending some other abbv's surounded by quotes:Code:printf("\n %-40s %-5s %3d - %3d %4.3f",team[mlnum[i]],abbv[mlnum[i]],mlw[mlnum[i]],mll[mlnum[i]],pct);
Example:
However this is only impacting some of the entries, others are appearing as just:Code:VIR""ABC""WES"
As they should...Code:VIR
Any idea what might cause this kind of an error?
Thanks again.
It could be that some of the strings in the abbv array are not 0 terminated, therefore the string would continue into the next array index until a 0 termination is found.
abbv[0] = VIR"
abbv[1] = "ABC"
abbv[2] = "WES"0
abbv[3] = "XXX"0
NULL termination missing from [0] and [1] but not [2] and [3].
printf("%s", abbv[0]);
would output VIR""ABC""WES"
So if I had used your algorithm to remove the quotes from the Abbreviation, fields as well, shouldn't that have added the termination string?Quote:
Originally posted by Scarlet7
It could be that some of the strings in the abbv array are not 0 terminated, therefore the string would continue into the next array index until a 0 termination is found.
I did check this in dbx and it appears that some of the strings have stuff on the end of them and aren't terminating where I expected it too...
so how would I add the 0 termination in the right spot?
Make sure that all the stings in the array are initialised to 0's before using.
char team[41][40] = {0};
If the strings are a fixed length then set the 0 at the end. i.e if all the strings are 3 then abbr[i][3] = 0
Also make sure that the array string length is big enough for the size of the string including the null at the end, add +1.
char team[41][40+1] = {0};
Heres a another way with arrays of strings to remove the ""...
Code:char s[4][13] = {0} ;
int i;
strcpy(s[0], "\"Virginia1\"");
strcpy(s[1], "\"Virginia2\"");
strcpy(s[2], "\"Virginia3\"");
strcpy(s[3], "\"Virginia4\"");
for(i=0; i < 4; i++)
*strstr( strcpy(&s[i][0], &s[i][1]), "\"") = 0;
for(i=0; i < 4; i++)
printf("%s\n", s[i]);
Here is a general purpose char remover.
THANKS A TON ALL IS WORKING NOW!
(as you can tell I'm happy)