Help with string in C

Hello, i want count chars in others char. For example:


Input:
111666349

find 6

output:

The number has 3 digits.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <string.h>

int main ()
{
  int i;
  char strtext[] = "111666349";
  char cset = '6';

  i = strspn (strtext,cset);
  printf ("The number has %d digits.\n", i);
  return 0;
}

Hi,

You need a different function, this one doesn't do what you think it does - even if you call it correctly. The function argument are both char arrays, not a char for one of them.

It is easy enough to loop through the string and count em :+)

Good luck !!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <iostream>

int main ()
{
  int number[10] = {0};
  
  char strtext[] = "11216663249";
  int length = sizeof(strtext)/sizeof(char);
  
  char key = '\0';
  std::cin >> key;
  
  for (int i = 0; i < length; i++)
     number[ strtext[i] - '0']++;
     
  std::cout << "Found: " << number[key-'0'];
  
  return 0;
}
Topic archived. No new replies allowed.