Problems with my c letter count program

This is a discussion on Problems with my c letter count program within the C Programming forums, part of the General Programming Boards category; Hi, I have been working on a program in c that takes an input and counts how many of each ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    4

    Problems with my c letter count program

    Hi, I have been working on a program in c that takes an input and counts how many of each letter there are and then lists them in the form eg:
    A: 56
    B: 68
    C: 43 etc etc

    Im haveing problems geting it to spit out the right results and is me all kinds of funny numbers and symbols and at the end saying segmentation fault(core dumped) heres my code, if you can see what i need to change please can you help. thankyou

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
    
    	int LetterCounts[26] = {0};
    	int c = 0, j = 0;
    
    	while ((c = getchar()) != EOF) {
        	if (isalpha(c)) {
    			c = toupper(c);
    		}
    		LetterCounts[c-'A']++;
    	}
    	for(j = 'A'; j < 'Z'; j++) {
    		printf(": %d\n", LetterCounts[j+'A'], LetterCounts[j]);
    	}
    	return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    Read contents of this link carefully.

    A development process
    S_ccess is waiting for u. Go Ahead, put u there.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're on the right track...
    Code:
    	while ((c = getchar()) != EOF) {
        	if (isalpha(c)) {
    			c = toupper(c);
    		}
    		LetterCounts[c-'A']++;
    	}
    And if they enter something like '1', it's going to point to out of bounds.

    for(j = 'A'; j < 'Z'; j++) {
    printf(": &#37;d\n", LetterCounts[j+'A'], LetterCounts[j]);
    }
    No, On the first iteration it would be, 'A' + 'A' which is defiantly not 0

    Perhaps something along the lines of?
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main(void)
    {
    	char input[BUFSIZ], cc = 0;
    	unsigned int letterFrequencies[26] = {0};
    	int i = 0;
    
    	/* read input */
    	fgets(input, sizeof(input), stdin);
    
    	for(i = 0; i < strlen(input); i++)
    	{
    		cc = input[i];
    
    		/* if it's [A,Z] */
    		if(isalpha(cc) != 0)
    		{
    			cc = tolower(cc);
    
    			/* cc is now lowercase and alpha */
    			letterFrequencies[cc - 'a']++;
    		}
    	}
    
    	/* print the letter counts */
    	for(i = 0; i < (sizeof(letterFrequencies) / sizeof(letterFrequencies[0])); i++)
    	{
    		printf("%c. %d\n", (char)('a' + i), letterFrequencies[i]);
    	}
    	
    	return 0;
    }
    Last edited by zacs7; 05-30-2007 at 03:18 AM.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    4
    that is really a lot more complicated than I need to have it and isnt what im trying to do, ie i want to list each letter on each line with how many of that letter are on each line next to it. Im sure my code is almost correct but its not quite working and would like to know where abouts its not.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,635
    Read a string. Then sort the string and count all the characters that are alphabetical.

    The filtering part isn't even that hard: just set up an array - so that each slot represents a letter - and keep adding one for each of that letter. When the letter is different, use the next slot, and repeat adding one like before. In the case where the letter isn't alphabetical, just do nothing until the character changes again. Repeat until bored.

    The sorting step should impose an order that allows a greedy algorithm like that one to work. All you really have to do is pay attention to what you're looking at.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't see why any sorting is needed. He just needs a tally alphabetic array, and he's got that.

    Cram55, what is your current code, and what errors are you getting NOW? I know you're very close.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,635
    > I don't see why any sorting is needed.

    Well, you don't have to sort perhaps, but greedy algorithms like that one usually work most correctly if there is an order. I always figured this problem was similar to breaking down a fistful of dollars into their largest or the available denominations. Besides, finding the proper index to put a letter count is easy and doesn't really rely on the given character set. And that's just good because it's portable, and an approach that works for multibyte strings quite well.

    Not that the OP is worried about that right now. To each his own.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    4
    yea i think everyones thinking a bit hard on this haha im sure it should be easy!!

    the code i have now is virtually the same im stuck:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
    
    	int LetterCounts[26] = {0};
    	int c = 0, j = 0;
    
    	while ((c = getchar()) != EOF) {
    			if (isalpha(c)) 
    				c = toupper(c);
    				LetterCounts[c-'A']++;
    	}
    	for(j = 'A'; j < 'Z'; j++)
    		printf("%c: %d\n", LetterCounts[j+'A'], LetterCounts[j]);
    	
    	return EXIT_SUCCESS;
    }
    and the error i get is:

    Code:
    i: -1081457140
    : 6121771
    : 1
    : -1081452740
    
    : 0
    : -1081452728
    ?: -1081452699
    K: -1081452666
    R: -1081452624
    _: -1081452605
    y: -1081452588
    : -1081452575
    � -1081452559
    � -1081452548
    : -1081452534
     : -1081452520
    : -1081452449
    !: -1081452412
    : -1081452394
    : -1081452362
    : -1081452340
    : -1081452310
    : -1081452299
    : -1081452288
    d: -1081452252
    Segmentation fault (core dumped)

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
    
    	int LetterCounts[26] = {0};
    	int c = 0, j = 0;
    
    	while((c = getchar()) != EOF)
    	{
    		if(isalpha(c))
    		{
    			c = toupper(c);
    			LetterCounts[c-'A']++;
    		}
    	}
    	for(j = 'A'; j <= 'Z'; j++)
    	{
    		printf("%c: %d\n", j, LetterCounts[j-'A']);
    	}
    	
    	return EXIT_SUCCESS;
    }
    Last edited by MacGyver; 05-30-2007 at 04:03 AM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if (isalpha(c)) 
    				c = toupper(c);
    /* This next line LOOKS like it will be executed only when the line above it is executed - but that's not true. 
    It's just indented so it looks that way. */
    				LetterCounts[c-'A']++;
    
    
    Looks wrong to me:
    
    Shouldn't it be:
    if (isalpha(c))  { 
    		c = toupper(c);
    		LetterCounts[c-'A']++;
    }
    You have it indented rather crazily to my eyes, but isn't this 2nd version of the if statement, what you want?
    Last edited by Adak; 05-30-2007 at 04:17 AM.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    4
    thanks for everyones help finally got the dam thing working!!!! sorry i must have been frustrating some people with my terrible indenting im new to all of this but thanks its good learning

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  2. Another program with more problems.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2003, 01:43 PM
  3. Count the number of letter "a" on a sentence
    By imbecile in C in forum C Programming
    Replies: 6
    Last Post: 07-27-2003, 02:32 PM
  4. Replies: 6
    Last Post: 07-10-2002, 07:45 PM
  5. word count program need a bit of help!
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 04-19-2002, 08:15 PM

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