Thread: Counting letters and digits

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Ventspils, Latvia
    Posts
    2

    Unhappy 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. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote 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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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++;
        }
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with counting words
    By geo_c in forum C Programming
    Replies: 7
    Last Post: 08-23-2004, 06:53 AM
  2. Splitting Digits and Letters
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-08-2002, 07:54 PM