944,107 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3308
  • C RSS
Oct 12th, 2007
0

Making a letter counting program

Expand Post »
I have been checking on others letter count programs and none so far are like mine so I'm now making my own thread. My assignment, which is due tomorrow! *Yikes* Is for me to make a text file in notepad and then important that to my code. Then, I has to ask the user to input a letter, then have my code count how many times that letter came up in my text file. My text file that I have attached is test.txt. Keep in mind, this is my first, and sorry, attempt at this. Here's what I have so far..
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main(int argc, char *argv[]) {
  4. printf("ECE 15 Assignment 2 \n");
  5. printf("My name \n");
  6.  
  7. FILE *fin;
  8. fin = fopen(argv[1], "r");
  9. int numlet, n;
  10. int h;
  11. char c;
  12. printf("Which letter would you like me to count in my text?: ");
  13. c = getchar();
  14.  
  15. fin = fscan(argv[1], c) = h;
  16.  
  17. printf("The letter inputed: %c happened %d times", c, h);
  18. return 0;
  19. }

Also, heres the skeleton to what my prof said I should have.

Comment "skeleton" for Assignment 2:
// Print: ECE 15 Assignment 2
// Print: Names of partners; end each name with \n
// Loop
// Open argv[1]
// Prompt user and read character
// If no character, break out of loop
// Initialize counter to 0
// Loop until end of file
// if characters match add 1 to counter
// Close file
// Print output line to user
// End of program


Thanks for any help guys, I'll be checking this site every 5 mins or so.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FEARmike21 is offline Offline
11 posts
since Oct 2007
Oct 12th, 2007
0

Re: Making a letter counting program

Well the first step is to being with the program which simply copies the file to somewhere else. If you can't manage that, the rest of the assignment is out of reach.
  1. FILE *in = fopen("file.txt","r");
  2. if ( in != NULL ) {
  3. int ch;
  4. while ( (ch=fgetc(in)) != EOF ) {
  5. fputc( ch, stdout );
  6. }
  7. fclose( in );
  8. }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 12th, 2007
0

Re: Making a letter counting program

Wait I'm a little lost here, the code you just posted is only to retrieve the text file? I'm using Pelles C if that makes a differnce. The hardest part I'm having is having the program search for the letter in my text.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FEARmike21 is offline Offline
11 posts
since Oct 2007
Oct 12th, 2007
0

Re: Making a letter counting program

Yes, now add your code to compare each letter with the one you've been asked to search for.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 12th, 2007
0

Re: Making a letter counting program

Okay so this is now my code...

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main(int argc, char *argv[]) {
  4. printf("ECE 15 Assignment 2 \n");
  5. printf("My name \n");
  6.  
  7. FILE *in = fopen("file.txt","r");
  8.  
  9. int numlet, n;
  10. int h;
  11. char c;
  12. printf("Which letter would you like me to count in my text?: ");
  13. c = getchar();
  14.  
  15. if ( in != NULL ) {
  16. int ch;
  17. while ( (ch=fgetc(in)) != EOF ) {
  18. fputc( ch, stdout );
  19. }
  20. fclose( in );
  21. }
  22.  
  23.  
  24.  
  25. printf("The letter inputed: %c happened %d times", c, in);
  26.  
  27. return 0;
  28. }

I feel as if im getting warming but still no cigar, I'm having troulbles find out how to search for the letter... any ideas or tips?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FEARmike21 is offline Offline
11 posts
since Oct 2007
Oct 12th, 2007
0

Re: Making a letter counting program

So add something like
if ( ch == c )
to compare, and then increment your count accordingly.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 12th, 2007
0

Re: Making a letter counting program

Okay, I get the idea, but what does the int ch stand for in this code though? Is that the number of characters in the text?

In that case, I was thinking going with a loop that if ch==0 then maybe maybe another int that counted how many times like i and go

if (ch ==0)
++i

What do you think?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FEARmike21 is offline Offline
11 posts
since Oct 2007
Oct 12th, 2007
0

Re: Making a letter counting program

> while ( (ch=fgetc(in)) != EOF )
Have you run a program containing only this line (and the fputc which followed it)?
Did you attempt to understand what it was doing?

ch in this context is every single character of the input file, one at a time.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 12th, 2007
0

Re: Making a letter counting program

Okay awsome, I took what you've said and I finally got the program to work, at least partially... I also need the program to only check the letter if one is entered, if one is not entered I need the program to close. Also in the program I need to set text.txt as a variable or something becasue tat my final statement I need to give what letter it was, how many times it came up, and what text file I searched so I'm assuming if I just type in test.txt in the printf thats not what he wants... Heres my working code so far, I havent put in my attempts to add in those two new things because everytime I do the code doesn't work so I just need some advice where to put the loop and how to structure it for the check EOF if the user doesn't put anything in.
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main(int argc, char *argv[]) {
  4. printf("ECE 15 Assignment 2 \n");
  5. printf("My name \n");
  6.  
  7. FILE *in = fopen("test.txt","r");
  8. int n;
  9. char c;
  10. printf("Which letter would you like me to count in my text?: ");
  11. c = getchar();
  12.  
  13. n=0;
  14. if ( in != NULL ) {
  15. int ch;
  16. while ( (ch=fgetc(in)) != EOF ) {
  17. fputc( ch, stdin );
  18. if (ch == c)
  19. ++n;
  20.  
  21. }
  22. fclose( in );
  23.  
  24.  
  25. }
  26.  
  27.  
  28.  
  29. printf("\nThe letter inputed: %c happened %d times in the file \n ", c, n);
  30.  
  31. return 0;
  32. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
FEARmike21 is offline Offline
11 posts
since Oct 2007
Oct 12th, 2007
0

Re: Making a letter counting program

> Also in the program I need to set text.txt as a variable or something
Like argv[1] as per your original post.

Without seeing some kind of attempt (working or not), it's hard to suggest what you might be doing wrong, either in terms of the approach, or merely from a syntax point of view.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Queue Pushed to Stack
Next Thread in C Forum Timeline: C expert help me! Write a TSR program that changes the background color ...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC