| | |
Making a letter counting program
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 11
Reputation:
Solved Threads: 0
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..
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.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> int main(int argc, char *argv[]) { printf("ECE 15 Assignment 2 \n"); printf("My name \n"); FILE *fin; fin = fopen(argv[1], "r"); int numlet, n; int h; char c; printf("Which letter would you like me to count in my text?: "); c = getchar(); fin = fscan(argv[1], c) = h; printf("The letter inputed: %c happened %d times", c, h); return 0; }
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.
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.
C Syntax (Toggle Plain Text)
FILE *in = fopen("file.txt","r"); if ( in != NULL ) { int ch; while ( (ch=fgetc(in)) != EOF ) { fputc( ch, stdout ); } fclose( in ); }
•
•
Join Date: Oct 2007
Posts: 11
Reputation:
Solved Threads: 0
Okay so this is now my code...
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?
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> int main(int argc, char *argv[]) { printf("ECE 15 Assignment 2 \n"); printf("My name \n"); FILE *in = fopen("file.txt","r"); int numlet, n; int h; char c; printf("Which letter would you like me to count in my text?: "); c = getchar(); if ( in != NULL ) { int ch; while ( (ch=fgetc(in)) != EOF ) { fputc( ch, stdout ); } fclose( in ); } printf("The letter inputed: %c happened %d times", c, in); return 0; }
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?
•
•
Join Date: Oct 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<string.h> int main(int argc, char *argv[]) { printf("ECE 15 Assignment 2 \n"); printf("My name \n"); FILE *in = fopen("test.txt","r"); int n; char c; printf("Which letter would you like me to count in my text?: "); c = getchar(); n=0; if ( in != NULL ) { int ch; while ( (ch=fgetc(in)) != EOF ) { fputc( ch, stdin ); if (ch == c) ++n; } fclose( in ); } printf("\nThe letter inputed: %c happened %d times in the file \n ", c, n); return 0; }
![]() |
Similar Threads
- pascal word and letter counting (Pascal and Delphi)
- C++ Program Q (C++)
- Making a .dat file (C++)
- need a good graphics program (Graphics and Multimedia)
Other Threads in the C Forum
- Previous Thread: Queue Pushed to Stack
- Next Thread: C expert help me! Write a TSR program that changes the background color ...
Views: 2058 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






