Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ANSI C : Stringy, opposite of concatenation type question.

Options
  • 02-05-2010 2:29pm
    #1
    Closed Accounts Posts: 6,408 ✭✭✭


    Ok, the user inputs six numbers 123456.

    I need to seperate out this input into 12, 34, and 56. Then decide if this is a valid date and print out the date.

    What's a good way to break the six digit input into 3 seperate pices of data?

    Thanks?


Comments

  • Registered Users Posts: 3,945 ✭✭✭Anima


    Is it always going to be 3 sets of 2 digit numbers?

    Could do somthing like this...
    [PHP]
    char user_data[7];
    int num1, num2, num3;
    int temp;
    .....
    scanf( &user_data, "%d" );

    temp = atoi(user_data);
    num1 = temp >> 4;
    num2 = (temp >> 2)&0x03;
    num3 = temp&0x2C;
    [/PHP]

    Haven't tested, may or may not work as expected.


  • Closed Accounts Posts: 6,408 ✭✭✭studiorat


    Anima wrote: »
    Is it always going to be 3 sets of 2 digit numbers?

    Could do somthing like this...
    [PHP]
    char user_data[7]; // sets character array? why 7 and not 6?
    int num1, num2, num3;
    int temp;
    .....
    scanf( &user_data, "%d" );

    temp = atoi(user_data); // never seen this before what does "atoi" do?
    num1 = temp >> 4;
    num2 = (temp >> 2)&0x03; // likewise with &0x03, looks like a memory location to me???
    num3 = temp&0x2C;
    [/PHP]

    Haven't tested, may or may not work as expected.

    Yeah it's 3 sets of two digit integers...

    Was also wondering why char user_data, and not int user_data...

    ok atoi converts the string into integers. can I use something like atoi(2) perhaps, for a two digit integer?


  • Registered Users Posts: 218 ✭✭Tillotson


    Or you could do something like this because a char in the range '0' - '9' can be converted to a number by subtracting '0':
    int main() {
            int i;
            char *c = "123456";
            int nums[3];
    
            for (i = 0; i < 3; i++) {
                    nums[i] = (*c - '0') * 10 + (*(c + 1) - '0');
                    c += 2;
            }
            // check for valid date here
            return 0;
    }
    


  • Registered Users Posts: 3,287 ✭✭✭padraig_f


    Sounds like a homework question, what have you got so far?


  • Closed Accounts Posts: 6,408 ✭✭✭studiorat


    it's a couple of tasks from the begining of the year, just got back to them, not done a jot yet...

    Working on DSP stuff at the mo, just thinking about this out loud before I start. Gotta get to it before tuesday...


  • Advertisement
  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    For strings:
    char* date = "123456"
    char day[3], month[3], year[3];
    sscanf(date, "%2c%2c%2c", day, month, year); 
    day[2] = month[2] = year[2] = '\0';
    

    For ints:
    int date = 123456;
    int day = date/10000;
    int month = date/100 & 0x7F;
    int year = date & 0x7F;
    


  • Closed Accounts Posts: 6,408 ✭✭✭studiorat


    Eh, Voila...
    int data;
    int day, month, year;
    
    main ()
    {
        printf ("Please enter six digits : \n");
        scanf ("%d", &data);
        day = data/10000; 
        month = (data/100)- (day*100); 
        year = (data) - (month*100) - (day *10000);
    
        printf ("You entered %d" ,data);
        printf ("The Date you entered was %d : %d : %d", day, month, year);
        return 0;
    }
    

    Thanks for getting the head goin'
    I used numbers, I have trouble thinking in characters and strings.
    I'm sure I'll be back with more later...

    edit: Yup! The only problem is if there is a single integer it doesn't put a zero infront of the integer. So I get 2 : Feb : 2 instead of 02 : Feb : 02


Advertisement