reading an array/string and sorting/ignoring the contents

This is a discussion on reading an array/string and sorting/ignoring the contents within the C++ Programming forums, part of the General Programming Boards category; guys, i have a problem that i want to read a string called Code: uncoded[250] its a hexadecimal string and ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    reading an array/string and sorting/ignoring the contents

    guys, i have a problem that i want to read a string called
    Code:
    uncoded[250]
    its a hexadecimal string and so contains both chars and ints.

    the string is something like :
    Code:
    RAWSTUFF 000.000 1343442 32523523 DEF457457457457475745HFE 3463436.000
    i would want to ignore the words and the 0000.000 but keep everything else. there is other stuff id like to ignore but i will figure that out after a kick in the right direction.

    p.s. the string is a variable which makes my life harder. however, the stuff i want to ignore remains the same. my attepmts are shown below.

    id just like to know how to ignore the stuff because i think i can re-arrange myself.

    all help appreciated.

    my attempts

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    /********************************************************
     * my_strchr -- Finds a character in a string.          *
     *      Duplicate of a standard library function,       *
     *      put here for illustrative purposes              *
     *                                                      *
     * Parameters                                           *
     *      string_ptr -- String to look through.           *
     *      find -- Character to find.                      *
     *                                                      *
     * Returns                                              *
     *      pointer to 1st occurrence of character          *
     *      in string or NULL for error.                    *
     ********************************************************/
    char *my_strchr(char * string_ptr, char find)
    {
        while (*string_ptr != find) {
     
           /* Check for end */
     
           if (*string_ptr == ' 0')
               return (NULL);       /* not found */
     
            ++string_ptr;
        }
        return (string_ptr);        /* Found */
    }
     
    int main()
    {
        char line[250];      /* The input line */
        char *first_ptr;    /* pointer to the writing*/
        char *last_ptr;     /* pointer to the zeros */
    	int *second_ptr;	// pointer to the 0 value
    
        fgets(line, sizeof(line), stdin);
     
        /* Get rid of trailing newline */
        line[strlen(line)-1] = 'RAWSTUFF';    
    	
    	    /* Get rid of 0s */
        line[strlen(line)-1] = '000.00000';  
     
        last_ptr = line;    // writing printed at new line//
     
        first_ptr = my_strchr(line, ' ');      // Find Spaces //
    	second_ptr = my_strchr(line, '0');      // Find 0s //
    
        /* Check for an error */
        if (first_ptr == NULL) {
            fprintf(stderr,
                "Error: Unable to find space in %s\n", line);
            exit (8);
    	if (second_ptr == NULL) {
            fprintf(stderr,
                "Error: Unable to find 0 in %c\n", line);
            exit (8);
        }
     
        *first_ptr = '\0';  // Zero out the slash //
    	*second_ptr = '00'; // Zero out the Zeros //
        ++first_ptr;        // move along array //
    	++second_ptr;        // move along array //
        printf("First:%s Last:%s\n", first_ptr, last_ptr);
        return (0);
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    RAWSTUFF 000.000 1343442 32523523 DEF457457457457475745HFE 3463436.000

    If your string always contains the word RASTUFF and the same number of digits where 000.000 is, then I would copy the stuff to the right of the second space into a new string by shifting to the left 17 spaces, or, in more generally, by the number of char to the left of and including the second space.
    Code:
    int length = strlen(originalString);
    int leftShift = 17;
    for(i = 0; i < strlen - leftShift; ++i)
      newString[i] = originalString[i + leftShift];
    newString[i] = '\0';
    alternatively, you could assign the address of first char after the second space to a pointer, say maybe:

    ptr = originalString + leftShift;

    or maybe

    ptr = &originalString[16];

    but then you have to be careful how you use the ptr.

    BTW, literal strings are indicated by double quotes and single characters by single quotes. And, your use of C style input/output suggests you may get better response by posting on the C board, rather than the C++ board.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

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