Thread: spaces

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Question spaces

    I'm nearly there with everything, I've been working on my program alot, and I've learnt so much from all your help thanks, couldn't have done it without.But still some trouble...

    When the user enters a string such as
    2,1,25 or like 2 1 25

    could I read each number into an array so its like:
    Code:
    array1 = {2, 1, 25};
    without the spaces or , having any effect.

    Because I then need to do a for loop to convert these to chars or ints.Any direction to where to start would be of great help.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    	int iA[3];
    
    	for(int i = 0; i < 3; i++) scanf("%d", &iA[i]);
    	for(int j = 0; j < 3; j++) printf("%d ", iA[j]);
    For commas, you may just fgets and strtok by comma.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    cool thanks,
    also,
    how do you capture the length of an int array
    ... if someone enters 2 1 15

    cus i want to do the for loop like this:

    for (i=0; i<length; i++)

    or could i do the above

    Code:
    int iA[3];
    
    	for(int i = 0; i < 3; i++) scanf("%d", &iA[i]);
    	for(int j = 0; j < 3; j++) printf("%d ", iA[j]);
    using a char array, and use strlen?
    though when i tried it myself i can't get it working.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If you don't know how many numbers a user will enter at runtime and you want all the numbers:

    * Not perfect, but easy: declare a big aray, say 1000 numbers. You can get the first 1000 numbers, should be more than you need

    * Use dynamic memory, eg. realloc() or a linked list, etc.
    Last edited by jafet; 04-07-2006 at 04:33 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  4. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM
  5. Replies: 5
    Last Post: 06-30-2003, 12:52 PM