Like Tree2Likes

Letter Frequency counting.

This is a discussion on Letter Frequency counting. within the C Programming forums, part of the General Programming Boards category; Originally Posted by Rezi I deffinitely get what you're both talking about, it's logical to keep each function short and ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Rezi View Post
    I deffinitely get what you're both talking about, it's logical to keep each function short and sweet, but I'm afraid this damn comparision between ch and let is still doing my head in.
    1) setup an int as a counter... int userchar = 0;
    2) get one character from your input file.
    3) on the very next line, do this...
    Code:
    if (ch == let)
      userchar++;
    That's it... all done.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Well, if you took the advice to heart, you'd write a small program that allowed you to play with ch and let and learn what's what.

    I guess I don't know exactly what you are wrestling with...

    Code:
     if(ch>='a' && ch==&let)
    What is this doing? Or, better, what do YOU want it to do? What's the ampersand for?

  3. #18
    Registered User
    Join Date
    May 2011
    Posts
    6
    That's exactly what I've been doing since yesterday afternoon.
    Code:
    	while((ch=fgetc(fpout))!=EOF)               	
    {						
    		if(ch == let)
    			nl++;
    	}
    Where nl has already been decalred as an int and = 0. I just ran into a coursemate who suggested creating another two ints, moving ch and let into them to compare the ints instead of chars. Gave that a go too and it didn't work, so I assume there's something wrong somewhere else. Unless of course my while loop is wrong, but the code is running through it.

    I appreciate the amount of help you've given me by the way, I noticed my thread's quite a lot longer than most of the others here ^^

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm... fgetc returns an int... check it out in your library documentation.

    There should be no reason to make copies of the existing variables, C is perfectly capable of comparing characters. A character is simply a short short int... usually 8 bits wide, or one byte. You can add, subtract, multiply and divide with chars just like you do with shorts, ints, long ints and long long ints. There's no magic...

    Now let me tell you this one more time... stop trying to reopen files to do single tasks, it's a disaster waiting to happen... First consider that your output file is all upper case... what happens if the user types a lower case character? Yeah that's right, the test fails...

    Look closely at where I am doing this.... (from your original code posting)


    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void main()
    {
    
    	FILE  *fpin, *fpout, *fprec;                        // declares two file streams, in and out
    	int tl, f, i=0;
    	int nl=0;
    	char ch, let=0;                                    // ch is variable to be used in conversion
    	char input[80], output[80], record[80];					// declared arrays to store file paths
    	printf("Input input file name:  \n");
    	gets(input);                                // input file location
    	printf("Input output file name: \n");
    	gets(output);								// output file location
    	printf("Input record file name: \n");
    	gets(record);								// record file location
    
    
    printf("Which letter would you like the frequency of?\n");
    scanf("%c", &let);
    
    
    	if((fpin = fopen(input,"r")) == NULL)		// confirms that file has been opened
    	{      
    		printf("Unable to open input file");
    		exit(0);
    	}
    
    	if((fpout = fopen(output,"w")) == NULL)		// confirms that file has been created
    	{    
    		printf("Unable to open output file\n");
    		exit(0);
    	}
    
    	tl=0;
    
    	while((ch=getc(fpin))!=EOF)                // read input file until the end of the file
    	{
    
    if (let == ch)
       nl++; 
    
    	    if((ch>='a' && ch<='z') | (ch>='A' && ch<='Z')) // ensures that only alphabetic characters are transferred
    		{   
    		putc(toupper(ch), fpout);               //function to change all the characters to upper case
    	    tl++;
    		}
    		
    	}
    
    	fclose(fpin);								// close input file
    	fclose(fpout);
    
    	fopen(output,"r");
    
    	if((fprec = fopen(record,"w")) == NULL)		// confirms that file has been created
    	{    
    		printf("Unable to open record file\n");
    		exit(0);
    	}
    
    	printf("Total number of letters in the file: %d\n", tl);
    The problem here is that you are adding something new... but ignoring the chance to integrate it into your *existing* code...
    Last edited by CommonTater; 05-06-2011 at 10:24 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lowercase letter frequency
    By pauzza in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 11:32 AM
  2. Counting Frequency using linked list
    By ChoCo in forum C Programming
    Replies: 16
    Last Post: 03-10-2010, 10:48 AM
  3. Character frequency counting
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 03-01-2010, 10:04 AM
  4. letter frequency
    By knoxville in forum C Programming
    Replies: 15
    Last Post: 08-01-2006, 02:31 AM
  5. optimizing loop (frequency counting)... HELP
    By skeptik in forum C Programming
    Replies: 22
    Last Post: 05-24-2004, 09:11 PM

Tags for this Thread


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