c - 'Character Array' of integers to 'Integer array' -


my requirement :

taking (unknown number) of integers input user , store them in numeric array.

eg: input: 25 3 4 78. want them stored in numeric array, num as

    num[0] = 25     num[1] = 3     num[2] = 4     num[3] = 78     . . .  

what did :

  1. stored input in character array.

    gets(arr); //takes input command prompt , stores in string

  2. then i'm trying find spaces , separate characters, turns out double digit number(say,25) stored 2 , 5 not 25.

how achieve this?

well, did not show code, i'll not provide code, i'll more happy provide flow-chart.

  1. define 1 array large enough (maybe change dynamic allocation later).
  2. take input user (not command line arguments) using fgets().
  3. use strtok() tokenize input using space delimiter.
  4. if non-null token received, use strtol() convert token int or long.
  5. if strtok() returned null means you've got tokens , input empty, finish up, got int array.

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

php - How do you embed a video into a custom theme on WordPress? -