Making a letter counting program

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 11
Reputation: FEARmike21 is an unknown quantity at this point 
Solved Threads: 0
FEARmike21 FEARmike21 is offline Offline
Newbie Poster

Making a letter counting program

 
0
  #1
Oct 12th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Making a letter counting program

 
0
  #2
Oct 12th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: FEARmike21 is an unknown quantity at this point 
Solved Threads: 0
FEARmike21 FEARmike21 is offline Offline
Newbie Poster

Re: Making a letter counting program

 
0
  #3
Oct 12th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Making a letter counting program

 
0
  #4
Oct 12th, 2007
Yes, now add your code to compare each letter with the one you've been asked to search for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: FEARmike21 is an unknown quantity at this point 
Solved Threads: 0
FEARmike21 FEARmike21 is offline Offline
Newbie Poster

Re: Making a letter counting program

 
0
  #5
Oct 12th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Making a letter counting program

 
0
  #6
Oct 12th, 2007
So add something like
if ( ch == c )
to compare, and then increment your count accordingly.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: FEARmike21 is an unknown quantity at this point 
Solved Threads: 0
FEARmike21 FEARmike21 is offline Offline
Newbie Poster

Re: Making a letter counting program

 
0
  #7
Oct 12th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Making a letter counting program

 
0
  #8
Oct 12th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: FEARmike21 is an unknown quantity at this point 
Solved Threads: 0
FEARmike21 FEARmike21 is offline Offline
Newbie Poster

Re: Making a letter counting program

 
0
  #9
Oct 12th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Making a letter counting program

 
0
  #10
Oct 12th, 2007
> 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 2058 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC