Thread: Count numbers in string

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    5

    Arrow Count numbers in string

    Hello, can you help me, how can i count numbers in string, for example "60 10" if i want result 70?

    Code:
    int main() {
    
       char r[5];
       int v;
    
       v = total("60 10", r, 5);
       if (v) {
         printf("%s\n", r);
       } else {
         printf("result is too great\n");
       }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use sscanf() or strtol() on your input string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    Where's the function definition for total (const char* , char* , int)? What is 'r' supposed to mean? What is 'v' supposed to mean? Start using descriptive variable names and get rid of the habit of using a bunch of random letters to denote your variable names.

    What do you intend to do by writing "if (v)"? It will only result to false when v == 0 and for all other cases, it will print r. You probably want it to be some kind of check.

    If you are going to be using spaces between each of the two numbers and null-terminated strings for sure, try writing a StringToInt conversion function.

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    5
    Ok, thank you. Can you help in source code? How would you write it?

  5. #5
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    Hallo Lolcoman!

    May be you can use strtok() declared in string.h (also cstring)
    you can see a example:
    Code:
    // splits a String into several parts
    #include <stdio.h>  //printf
    #include <stdlib.h> //atoi EXIT_SUCCESS
    #include <string.h> // strcpy, strtok
    
    // argument 1(*var)= strint to split
    // argument 2=delimiter
    // argument 2 = 2 dimensional array where the tokens are copied
    // Return-Value: number of tokens where found
    int mixed(char *var, char *delimiter, char numbers[20][30])
    {
     
     printf ("the delimiter is: %s(space)\nTokens:\n", delimiter);
     
      int retval = 0, i = 0;
      char * d;
      d = strtok (var, delimiter);
      
      while (d != NULL)
      {
        strcpy(numbers[i], d); 
        printf ("i: %2d      String: %s\n", i, numbers[i]);
        i++;
        d = strtok (NULL, delimiter);
       retval++;
      }
     
     return retval;
    }
    
    int main(int argc, char **argv)
    {
     char test[] = {"60 10 Winston Charly John 44 67 Henry 0 Mary-Jane_Lovley"};
     char delimiter[] = " ";
     char numbers[20][30] = {0};
     int ival = 0, i,j, result_a, sum = 0;
      
     printf ("String to split into tokens:\n%s\n\n", test);
     result_a =mixed(test, delimiter, numbers);
     
      
     printf ("\nNumber of tokens: %d\n-----tokens splited into characters-------\n", result_a);
     for (i = 0; i < result_a; i++)
      {
       printf("i: %2d     ", i);
        for (j = 0; j < 30; j++)
         printf(" %c ", numbers[i][j]);     
       printf("\n");
      }
      printf ("-----tokens converted into int-values------\n");
     
      for (i = 0; i < result_a; i++)
       {
        ival = atoi(numbers[i]);
        sum += ival;
        printf("ival: %d\n", ival);
       }
        
      printf("\nsum of all int-values:   %d\n\n", sum);
      
    return EXIT_SUCCESS;
    }
    This code is not without failures because the argument number 3,
    the char array must have enough size if you don't want to have a
    overflow.

    output of the example:
    Code:
    String to split into tokens:
    60 10 Winston Charly John 44 67 Henry 0 Mary-Jane_Lovley
    
    the delimiter is:  (space)
    Tokens:
    i:  0      String: 60
    i:  1      String: 10
    i:  2      String: Winston
    i:  3      String: Charly
    i:  4      String: John
    i:  5      String: 44
    i:  6      String: 67
    i:  7      String: Henry
    i:  8      String: 0
    i:  9      String: Mary-Jane_Lovley
    
    Number of tokens: 10
    -----tokens splited into characters-------
    i:  0      6  0                                                         
    i:  1      1  0                                                         
    i:  2      W  i  n  s  t  o  n                                               
    i:  3      C  h  a  r  l  y                                                 
    i:  4      J  o  h  n                                                     
    i:  5      4  4                                                         
    i:  6      6  7                                                         
    i:  7      H  e  n  r  y                                                   
    i:  8      0                                                           
    i:  9      M  a  r  y  -  J  a  n  e  _  L  o  v  l  e  y                             
    -----tokens converted into int-values------
    ival: 60
    ival: 10
    ival: 0
    ival: 0
    ival: 0
    ival: 44
    ival: 67
    ival: 0
    ival: 0
    ival: 0
    
    sum of all int-values:   181
    If you would change some things in 'mixed' you could
    save directly the converted int-values into a int-array.
    But that will be your work.
    Sorry but my English is not very good, so i prefer to take examples.
    Last edited by rusyoldguy; 11-26-2019 at 12:31 PM.

  6. #6
    Registered User
    Join Date
    Nov 2019
    Posts
    5
    Thank you very much! Why can i use this terms?

    Code:
    printf("result: %d\n", sum);//vypsaní vysledku
    
        int pole[]={sum};
        if (pole < 1)
        {
            printf("1");
        }
        else
        {
            printf("0");
        }
    
        return (EXIT_SUCCESS);

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    5
    I want to number (sum 10) into array for example [5] and if it smaller return 1 else return 1. If you know what i mean? Thank

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: count even and odd numbers
    By taigaicon in forum C Programming
    Replies: 1
    Last Post: 03-11-2011, 02:19 PM
  2. Trying to count numbers in file
    By ammochck21 in forum C++ Programming
    Replies: 25
    Last Post: 11-17-2006, 12:32 PM
  3. count numbers input
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-10-2002, 09:48 AM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread