strcpy question

This is a discussion on strcpy question within the C Programming forums, part of the General Programming Boards category; hello all, i'm trying to copy a string leaving out some of the characters. example: $4,000.00 > 4000.00 i need ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    strcpy question

    hello all,
    i'm trying to copy a string leaving out some of the characters.
    example:

    $4,000.00 > 4000.00
    i need to get rid of the '$' and ','.
    thank you in advance.
    the cman

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    look up ispunct() in your helpfiles.
    im off to bed. cant give example too tired.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    3
    Thank You. ispunct worked fine BUT
    I need to preserve the '.' punct.
    Example of my code output
    input = dollar$4,000.00
    current output = 400000
    DESIRED OUTPUT = 4000.00
    anyone got any ideas????
    thanks

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    char str1[100];
    char str2[200];
    char str3[200];

    int main( void )
    {
    int i;
    int a = 0;
    scanf("%s", str1);
    strcpy(str2, str1);
    for( i = 0; i < strlen(str2); i++ ) {
    if( isalpha(str2[i])) {
    continue;
    }
    if( ispunct( str2[i]) ) {
    // printf( "Char %c is a punctuation character\n",
    //str2[i] );
    continue;
    } else {
    // printf( "Char %c is not a punctuation character\n",
    // str2[i] );
    str3[a] = str2[i];
    a++;
    }
    }
    printf("str1= %s\n", str1);
    printf("str2=%s\n", str2);
    printf("str3=%s\n", str3);

    return EXIT_SUCCESS;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    ptr = strchr( inputstring, '$' );
    if( ptr != NULL )
        ptr2 = strchr( ptr, ' ' );
    if( ptr2 != NULL )
        strncpy( buffer, ptr, ptr2-ptr );
    That should suffice. You could nest your if statements, but I'll leave the fine tuning to you.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    You could write your own strcpy function that passes the chars to not to be copied.

    Code:
     
    void mystrcpy(char *s1, char *s2, char *cskip)
    {
        while(*s2)
        {
            char *c = cskip; int iskip=0;
            *s1 = *(s2++);
            while(*c)
                if(*s1 == *(c++))
                    iskip=1;
            if(!iskip) s1++;
        }
        *s1 = 0;
    }
    
    void main() 
    {
        char s1[20], *s2 = "$4,000.00";
        mystrcpy(s1, s2, "$,");
        printf("%s %s\n",s1, s2);
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main()
    This is wrong, main only returns an int. To be more specific, the correct definition of main when accepting no arguments is:
    Code:
    int main ( void )
    The other portable definition of main being one that takes two arguments:
    Code:
    int main ( int argc, char *argv[] )
    Where the types and identifiers of the parameters can be anything equivalent to the above.

    -Prelude
    Last edited by Prelude; 03-07-2003 at 10:53 AM.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    >>void main()
    yes, you're quite right, here's the reason why.

    http://users.aber.ac.uk/auj/voidmain.shtml

    Cheers....

  8. #8
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    What about something like?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void)
    {
      char string[] = "$4,000.00", *ptr;
    	
      int i, j;
    	
      j=0;
    	
      /* other code for inputting string */
    	
      if((ptr = (char *) malloc(sizeof(char) * strlen(string))) == NULL)
      {
        perror("Couldn't allocate memory");
        return -1;
      }		
    	
      for(i=0; string[i] != '\0'; i++)
        if(isdigit(string[i]) || string[i] == '.')
          ptr[j++] = string[i];
    	
      ptr[j] = '\0';
    			
      printf("%s\n", ptr);
    	
      return 0;
    }
    Hammer... ah, a chance for me to try pointers... is it close?

    Well I suppose that the string could have multiple occurrences of '.' so maybe it's not the best idea afterall.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is it close?
    Yes, but there are a few gotchas:

    >if((ptr = (char *) malloc(sizeof(char) * strlen(string))) == NULL)
    There's no need for sizeof(char) since it always evaluates to 1, and strlen returns a size_t value. Using sizeof(char) serves no purpose in this case. You shouldn't cast malloc since it may hide the error of not including stdlib.h, and you forgot to include string.h yet used strlen, this results in undefined behavior. And lastly, you forgot to att 1 to the size you ask for to account for the nul character. strlen does not include it.

    >return -1;
    -1 is an implementation defined return value, it is not portable. A better way would be to use EXIT_FAILURE which is defined in stdlib.h.

    >return 0;
    Since you used EXIT_FAILURE before, using EXIT_SUCCESS here would be good for consistency. You also neglected to free the memory you allocated previously, this is a bad thing.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by Prelude
    >is it close?
    Yes, but there are a few gotchas:
    Thanks for that Prelude. Some of those flubs just follow my signature.

    free(ptr); there

    What I'd really like to see is a textbook written by the people that have helped me the most, if not directly, then by reading their replies to others -- Salem, Hammer, Prelude, and Quzah. If Cela is still around, then add that person too.

    Without these folks, I'd still be using void main.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21