Thread: How to edit a string...

  1. #1
    Thanks4help
    Guest

    How to edit a string...

    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.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    This would not work if one of the " was missing i.e crash
    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;
    }
    The following is safer.
    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);
    Last edited by Scarlet7; 05-16-2003 at 04:07 PM.

  3. #3
    Thanks4help
    Guest

    Thanks..

    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:

    Code:
    /* Remove the Quotes */
    
      for (i=1;i<number+1;i++)
      {
        *strstr(strcpy(team[i][j],team[i][j+1]), "\"") = 0;
      }
    And I got two errors:
    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.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    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");
    
    }

  5. #5
    Thanks4help
    Guest

    Thanks a ton.

    I just saw the second way you tried it.


    That one seems to have worked THANKS!

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Code:
        char *s;
    
        for (i=1;i<number+1;i++)
        {
             s = team[i];
             *strstr( strcpy(s, s+1), "\"") = 0;
        }
    Last edited by Scarlet7; 05-16-2003 at 04:27 PM.

  7. #7
    Thanks4help
    Guest

    Same Program new problem.

    Ok... one last question if you've got a minute.

    When I'm outputting my data now I'm using code that looks like:

    Code:
    printf("\n %-40s %-5s   %3d - %3d   %4.3f",team[mlnum[i]],abbv[mlnum[i]],mlw[mlnum[i]],mll[mlnum[i]],pct);
    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:

    Example:

    Code:
    VIR""ABC""WES"
    However this is only impacting some of the entries, others are appearing as just:

    Code:
    VIR
    As they should...

    Any idea what might cause this kind of an error?

    Thanks again.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    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"

  9. #9
    Thanks4help
    Guest
    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.
    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?

    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?

  10. #10
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    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};

  11. #11
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    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]);
    Last edited by Scarlet7; 05-16-2003 at 05:18 PM.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here is a general purpose char remover.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    Thanks4help
    Guest

    THANKS

    THANKS A TON ALL IS WORKING NOW!

    (as you can tell I'm happy)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM