Skip to content

Instantly share code, notes, and snippets.

@DhanushkaSandakelum
Created December 26, 2020 08:00
Show Gist options
  • Save DhanushkaSandakelum/7c41d87ae0b1ed4fc3cc5c154e16e25b to your computer and use it in GitHub Desktop.
Save DhanushkaSandakelum/7c41d87ae0b1ed4fc3cc5c154e16e25b to your computer and use it in GitHub Desktop.
This is a simple code for take any type of a input and count the numbers of it using C
// first make sure add there libraries correctly
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char mem[1000];
int numCount[10];
scanf("%[^\n]", mem);
// to initialize numCount array to 0
for(int i = 0; i < 10; i++)
{
numCount[i] = 0;
}
// counting letters
for(int i = 0; mem[i] != '\0'; i++)
{
if(isdigit(mem[i]))
{
int t = (int)mem[i] - 48;
numCount[t] = numCount[t] + 1;
}
}
// print the counted result
for(int i = 0; i < 10; i++)
{
printf("%d ", numCount[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment