I need help counting letters., folk!

This is a discussion on I need help counting letters., folk! within the C Programming forums, part of the General Programming Boards category; Hello, I am writing a program using getchar() that reads characters from the keyboard until the # sentinnel character in ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question I need help counting letters., folk!

    Hello,
    I am writing a program using getchar() that reads characters from the keyboard until the # sentinnel character in encountered. The program should count the number of occurrences of the letters a, b, and c.

    I am goig pretty good here but ran smack into the damn wall with the last sentence. where can i get help from here. How do i get it to count the number of letters a,b, and c?
    TOTALLY BODACIOUS!

    #include<stdio.h>
    #include<ctype.h>

    int found_next_word(void);

    int main(void)
    {
    int word_count=0;
    while (found_next_word() == 1)
    ++word_count;
    printf("Number of words = %d\n\n", word_count);
    int found_next_word();
    return 0;
    }

    int found_next_word(void)
    {
    int c;

    while (isspace(c = getchar()))
    ;
    if (c == '#')
    printf("You can not use this # symbol. Please try again.\n");
    while (c == '#')
    return 0;
    if (c != EOF) {
    while ((c = getchar()) != EOF && !isspace(c))
    ;
    return 1;
    }
    return 0;
    }
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I dont see any attemp to count letters, just words. Give it a shot, and ask help when you have a specific question

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int a[256]={0},b;
    FILE*c=fopen("somefile","r");
    while((b=fgetc(c))!=EOF)letters[a]++;
    fclose(c);

    Nice and compact.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by quzah
    int a[256]={0},b;
    FILE*c=fopen("somefile","r");
    while((b=fgetc(c))!=EOF)letters[a]++;
    fclose(c);

    Nice and compact.
    Maybe compact, but not quite correct You've confused letters, b and a. Here's a working version:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int a[256]={0},b;
    	FILE *fp;
    	
    	if ((fp = fopen("test.txt","r")) == NULL)
    	{
    		perror("open failed");
    		return (EXIT_FAILURE);
    	}
    	
    	while((b=fgetc(fp))!=EOF)
    		a[b]++;
    	
    	fclose(fp);
    	
    	for (b = 0; b < 256; b++)
    		printf ("0x%02x %d\n", b, a[b]);
    
    	return (EXIT_SUCCESS);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    getchar() reads a character from the standard input, usually the
    keyboard. getchar will also 'echo" the typed character to the standard output, usually the user screen.


    I couldn't tell exactly how you wanted this program to work but here's an example.


    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int char_count = 0;
      char next_char = '\0';
    
      printf(" Enter characters but not the # sign <>");
      for(;;)
      {
        next_char = getchar();
        switch(next_char)
       {
        case 'a': case 'b': case 'c':
        char_count++;
        printf(" a,b,c count = %d\n", char_count);
        break;
    
        default: break;
       } // end switch(next_char)
    
       if(next_char == '#')
       {
        puts(" I said not to enter the # sign!");
        break;
      } // end if(next_char == '#')
     } // end for(;;)
    } // end int main(void)
    Last edited by jerryvtts; 07-15-2002 at 12:07 AM.

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by Hammer
    [B]
    Maybe compact, but not quite correct You've confused letters, b and a. Here's a working version:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int a[256]={0},b;
    	FILE *fp;
    	
    	if ((fp = fopen("test.txt","r")) == NULL)
    	{
    		perror("open failed");
    		return (EXIT_FAILURE);
    	}
    	
    	while((b=fgetc(fp))!=EOF)
    		a[b]++;
    	
    	fclose(fp);
    	
    	for (b = 0; b < 256; b++)
    		printf ("0x%02x %d\n", b, a);
    
    	return (EXIT_SUCCESS);
    }
    Correct only under the assumption that char is unsigned and UCHAR_MAX is 255, on some implementations char may be signed , so a negative value of b will cause the program to crash
    The one who says it cannot be done should never interrupt the one who is doing it.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    He may be right. EOF is -1.
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I was wrong , getchar() returns then next byte read as an unsigned char cast to an int , so such a problem would never arise .

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. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 09:15 PM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Help with counting letters
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2003, 03:49 PM
  5. Counting letters in window
    By Soldier in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2002, 10:34 PM

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