Suppose there are numbers 156, 13, 42, 12, 123, and I need their number - 5.

What function to use?

strlen(int_a); или int_a.length(); 
  • 1. If you are writing in C ++, and not C, then it will be more appropriate to use length (). 2. None of these functions will solve your problem, however, like other built-in functions. You will have to write your own function that will parse the string and count the number of numbers. - Alex Krass
  • @ bellator001, If ​​you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

3 answers 3

It depends on how these numbers are organized.

If this is an array whose size is known at compile time:

int a[]={156, 13, 42, 12, 123};

you can use the expression sizeof a / sizeof a[0] .

If they are in a standard container like std::vector<int> , then there is a member function size() .

  • one
    Judging by the fact that the author asks about the functions strlen and String.lenght (), this is most likely an input string. Although yes, it is better to clarify the moment in which form the data is stored. - Alex Krass

If the format is exactly this - "156, 13, 42, 12, 123", then count the number of commas and increase by one.

    @ bellator001 , you were rightly told that there is no one library function. Depending on the format of the data in the string will have to use different functions.

     avp@avp-xub11:hashcode$ cat cc #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> char * strtos (char **p, const char *delim) { char *word = 0; if (*((*p) += strspn(*p, delim))) { word = *p; (*p) += strcspn(*p, delim); } return word; } int main (int ac, char *av[]) { int rc = 1, n = 0, d; if (av[1]) { if (isdigit(av[1][0])) { FILE *f = fmemopen(av[1], strlen(av[1]), "r"); while (fscanf(f, "%d", &d) == 1) n++; rc = !feof(f); fclose(f); } else { char *w, *ep = av[1]; int err = 0, l; while (w = strtos(&ep, " ,")) { char *p, buf[(l = ep - w) + 1]; strncpy(buf, w, l); buf[l] = 0; strtol(buf, &p, 10); if (!*p) n++; else err++; } rc = err > 0; } printf ("%d numbers (%s)\n", n, rc ? "Err" : "OK"); } return rc; } avp@avp-xub11:hashcode$ g++ cc avp@avp-xub11:hashcode$ ./a.out ' 1, 2, 33, 4, 616' 5 numbers (OK) avp@avp-xub11:hashcode$ ./a.out '1, 2, 33, 4, 616' 1 numbers (Err) avp@avp-xub11:hashcode$ ./a.out ' 1 2 33' 3 numbers (OK) avp@avp-xub11:hashcode$ ./a.out '1 2 33' 3 numbers (OK) avp@avp-xub11:hashcode$ 

    I intend not to write comments in this program, but gave its output for various inputs in order to somewhat simplify the @ bellator001 parsing process.

    • Not very clear code, for example, it is not clear why there is an error in the second example. In addition, single-letter variables and the non-standard function fmemopen . - Im ieee
    • @Im ieee, the error in the second example, as it is easy to notice, after reading the code, arises from fscanf () for reading numbers. Actually, if (isdigit... and controls the choice of the method of their reading. As for the non-standard fmemopen - yes, Windows fans are not lucky ... - So the choice is obvious - use strtos() (or your analog). - avp