Thread: How to find out how much numbers are in array

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    6

    Unhappy How to find out how much numbers are in array

    Hi,guys!
    I would like to ask you : How to find out how much numbers are in array except space? My program will find out length of array, until user input will be space and that is the problem. So if I have inserted "10 20 300", the length will be 2. I have expected 3,like three numbers,which were inserted.And I don't know,how much numbers will user type in. I have already tried strlen(p),it was the same thing. Can you help me? Thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
    char p[1000];
    char val[1000];
    int length = 0;
    char* itr;
    
    fgets(p, sizeof(p), stdin);
    
    itr = strtok(p, " ");
    while(itr!=NULL)
        {
            val[length]=atof(itr);
            itr = strtok(NULL, "");
            length ++;
            
    
        }
        
    itr = strtok(p, " \n");
    
    
    printf("%d",length);
    
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    First, you need to indent your code properly. For example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        char p[1000];
        char val[1000];
        int length = 0;
        char* itr;
    
        fgets(p, sizeof(p), stdin);
    
        itr = strtok(p, " ");
        while (itr != NULL)
        {
            val[length] = atof(itr);
            itr = strtok(NULL, "");
            length++;
        }
        itr = strtok(p, " \n");
    
        printf("%d", length);
    
        return 0;
    }
    Next, do you actually need to care if the input consists of numbers? If not, then you should just check if the token is non-empty, and if so, include it in the count. Note that you have a typo error, i.e., this:
    Code:
    itr = strtok(NULL, "");
    should be:
    Code:
    itr = strtok(NULL, " ");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    6
    Yes, the typo error was behind my troubles, thank you very much !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 03-30-2014, 06:30 AM
  2. how to find numbers in a char array.
    By amzc in forum C Programming
    Replies: 5
    Last Post: 10-14-2012, 10:33 PM
  3. How to find the smallest 5 numbers in an array?
    By atac in forum C Programming
    Replies: 4
    Last Post: 08-28-2012, 02:27 AM
  4. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  5. find and replace duplicate numbers in array
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2009, 11:05 AM

Tags for this Thread