Counting letters in each state

This is a discussion on Counting letters in each state within the C++ Programming forums, part of the General Programming Boards category; Here's the program: Code: #include <iostream> #include <iomanip> using namespace std; int main() { const char states[50][25] = { "Alabama", ...

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Counting letters in each state

    Here's the program:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    
       const char states[50][25] = { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware",
       "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming" };
       
       const char abb[50][3] = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" };
    
       char alphabet[54]={"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    
       
       
       cout << "Select a state: " << endl;
       
       for ( int count = 0; count < 51; count++ ) { // count represents corresponding state numbers
    
          if ( count != 0 ) { // eliminates the zero in the array
          cout << setw( 10 ) << count << setw( 20 ) << states[count - 1]; // counts and ties states to 1 - 50
    
          if ( count % 2 == 0 ) // if count is divisible by 2, begin new line of output (creates two columns)
             cout << endl;
    
          } // end if structure
    
       } // end for structure
       
       cin >> count; // state number entered and matches count 
    
    ***********************************
       int lettercount[53];
    
          for ( int i = 0 ; i < 56 ; i++ )
    
          lettercount[count][i]]++;
    
    ***********************************
       cout << "You've selected " << states[count-1] << " state." << endl;
       cout << "Abbreviation is " << abb[count-1] << " and state name length is " << lettercount << endl;
    
           
    return 0;
    
    }
    This code works until I try to implement counting the letters of the states (between the asterisks).
    Arrays are still very new (and complicated) to me.
    Could someone point me in the right direction?
    Last edited by Kayoss; 11-16-2005 at 12:38 PM.
    THE redheaded stepchild.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    You could just use strlen to get the length of the string.

    If you need to calculate the length yourself, then you should loop through each character in the state name. In other words, the for loop should not end when i < 56 is false, it should end when the ith letter in the state name is not null is false.

  3. #3
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Unfortunately we haven't covered strlen just yet; we have to have the program count it.

    Code:
    if ( states != '\0' )
       for ( lettercount = 0; lettercount < states; lettercount++ );
    I've come up with this, knowing a counter needs to count each letter and output it....however "states" is declared char, not int, and I'm not sure how to tie it to an int, if that makes any sense.
    THE redheaded stepchild.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    What you want to do is check the character at the lettercount position to see if it is '\0'. So first step is to figure out the syntax for the character at position lettercount in the state at position count. Once you've got that syntax down, you can worry about how to formulate the loop.

  5. #5
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Im not entirely sure I understand. You want to get a count of every letter in the states array? Thats what I got from it anyhow..

    a couple ideas... (sorry no code )
    1. gonna need 2 loops, an outer one to traverse the states array and the inner one to count each char in the current state
    2. you dont need to do any conversion from alpha to numeric, just create an int, init it to 0, and every time you find a valid char increment
    3. to see when you are at the end of a states name (between quotes), test for null...

    here is example of #3

    if(states[i][0])
    if(states[i][j])

    test for valid char or test for null, either way it will tell you when either you have a valid string to read from or you are at the end of the current string

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Here ya go, hope it helps and your understand. Just put it between your **** lines,
    Code:
           int lettercount = 0; 
       char check = states[count-1][0]; 
       int counter = 0;
    
    	while (check != '\0')
    	{
    		
    		counter++;
    	
    		lettercount++;
    
    			if (check == ' ') lettercount--; //spaces are not letters.
    		
    
    		check = states[count-1][counter];
    		
    
    	}
    P.S. In your array that holds the state names...why does each one hold 25 char? You know how long the longest state is. Why waste the memory? You can make to15 to be safe (it is always good to be safe).
    Last edited by Enahs; 11-16-2005 at 05:08 PM.

  7. #7
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    You've worded my thoughts exactly Xhi!

    Code:
     check = states[count-1][counter];
    Enahs, this is exactly what I was trying to accomplish. I knew it was a loop within a loop but wasn't sure how to "tie" them together and this does it beautifully. Thank you both very much!
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting letters in string
    By CGbiginner in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 11:47 AM
  2. input/switch statement issues
    By peanut in forum C Programming
    Replies: 5
    Last Post: 10-27-2006, 02:58 PM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  5. I need help counting letters., folk!
    By correlcj in forum C Programming
    Replies: 7
    Last Post: 07-14-2002, 12:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21