Thread: Counting Occurances of a Character

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    25

    Counting Occurances of a Character

    Hi,

    I need help trying to do the following:
    Basically, I am trying to count the occurrences of each character in a text file. I'm reading in line by line, and then in each line, analyzing character by character.

    I'm maintaining an array of the counts.

    The conversion of the character to ascii and then ascii to its proper index is working, but when I try to print out the contents of the array, I'm getting ridiculous numbers. Here's my code:

    Code:
    
    int autoGuess() {
    
    int r, x, size, asciiEquiv;  
    int charCount[26];
    
    
    for (r = 0; r < lineCount ; r ++) /* Outer Loop Reads Line By Line */
    
    {
    
    	for (size = 0; size < sizeof(stringArray[r]); size++) {      /*Inner Loop Reads character by character */
    
    
    		if (isalpha((stringArray[r][size])))
    
    		{
    
    			asciiEquiv = (int)stringArray[r][size];
    		/*	printf("%d " , asciiEquiv); It is converting the characters to their respective ASCII codes properly */
    			place = asciiEquiv - 97; 
    		/*	printf("%d ", place);  Converting to Index Properly */
    		    charCount[place]++;
    			printf("%d " , charCount[place]);  // Something happens here
    			
    
    	    }
    
             
    	
    	}
    
    
    	
    
    }
    
    
    for (x = 0; x < 26 ; x ++) {
    
    //printf("%d ", charCount[x]); 
    	getchar();
    
    
    }
    
    
    }
    Thanks!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're never initializing you're array to all 0's. Try this:
    Code:
    int charCount[26] = { 0 };
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Alright, that works !

    Elementary mistake..

    Thanks

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    for (size = 0; size < sizeof(stringArray[r]); size++) {      /*Inner Loop Reads character by character */
    Are you sure that you want to use sizeof() here?

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Why would that be an issue?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    This is highly dangerous:
    Code:
    place = asciiEquiv - 97; 
    		/*	printf("%d ", place);  Converting to Index Properly */
    		    charCount[place]++;
    You are assuming that all characters are guaranteed to be 'a' to 'z'. That is, lowercase only... not spaces, no special punctuation.
    sizeof(), as was pointed out, will make it loop to the end of each row regardless of whether there is valid data. The rows may contain uninitialized (garbage), or zeroes, which would put your index outside the valid range.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 09-28-2010, 11:07 AM
  2. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM