# Thread: Counting letters and digits

1. ## Counting letters and digits

I have problem - i can't find way to count letters and digits from arrary. I need to make it for school at the begin you enter some kind of symbols random. Example: adsadDEARFasda3213Eras Then you enter symbol for example: a, first - programm counst how many time you have entered a -> gives to you result, then programm counts how many letters are in your arrary and print out and the same with digits, but i can't find fuction whom can count digits and letters independent i have found only function strlen(arraryName) which counts all elements in arrary...

In fact problem is I need function which counts 1. small and big letters 2. counts how many digits are in my arrary

In fact i need two separate functions...

Code of my programm:
Code:
```#include <iostream>
#include <iomanip>
using namespace std;

void svitra();

int main()
{
char simbv[50];
char simb;
int daudz=0;

cout<<"Ievadiet simbolu virkni, lai taja butu:"<<endl;
cout<<setw(33)<<"simbols,kas atkartojas;\n"<<setw(17)<<"cipari;\n"<<setw(22)<<"lielie burti."<<endl;
svitra();
cout<<"\n==>"; cin.get( simbv, 50, '\n'); // Enter some random letters small and big and digits
svitra();
cout<<"\nIevadiet simbolu, lai: \n"<<setw(45)<<"noteiktu, cik reizes tas atkartojas\n"<<setw(28)<<"to aizvietotu ar *."<<endl;
svitra();
cout<<"\n==>"; cin>>simb; // enter symbol which will be showed how many time
svitra();
for ( int i=0; i<=50; i++ )
if ( simbv[i] == simb )
daudz++;
cout<<"\nk ==> "<<daudz<<" reizes!"; // how many times you have entered symbol

cout<<endl;
system("pause");
return 0;
}

void svitra()
{
for ( int i=0; i<60; i++)
cout<<"-";
}```

2. Originally Posted by FeNCinGeR
I have problem - i can't find way to count letters and digits from arrary. I need to make it for school at the begin you enter some kind of symbols random. Example: adsadDEARFasda3213Eras Then you enter symbol for example: a, first - programm counst how many time you have entered a -> gives to you result, then programm counts how many letters are in your arrary and print out and the same with digits, but i can't find fuction whom can count digits and letters independent i have found only function strlen(arraryName) which counts all elements in arrary...

In fact problem is I need function which counts 1. small and big letters 2. counts how many digits are in my arrary
Every character has a value from 0-255.
Set up an array large enough for each character, zero the array.
Now use each input character as an index into your array and increment that location.
When you are done with each input character, your array will contain the counts of each character.

Oh, and please format your code. Indent at each open brace and unindent at each close brace.

3. There are facilities in the language that make these things much easier than you would think. Learn to use the string container. You don't have to worry about an arbitrary size limitation like you do with a character array. There are also functions in the Standard Template Library (STL) that you can use to help with the counting.

Enter the random numbers/characters into a string. You can call the string's own length member function to get the length of the sequence you entered. You can call the STL count function to count how many of a certain element exist in the string container.

4. with char arrays you can use the isalpha() and isdigit() functions, by example:

Code:
```for(int i = 0; i < lengthofarray; i++)
{
if(isalpha(array[i]))
{
letter++;
}
else if(isdigit(array[i]))
{
digit++;
}
}```