Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

ANSI C : Stringy, opposite of concatenation type question.

  • 02-05-2010 02: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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 3,335 ✭✭✭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