January 20th, 2014, 11:16 PM
Printing repeating characters between text files
I'm brand spanking new to programming and to the site. I seem to be picking things up okay, but I'm having a little trouble with part of my vowel counting program (yeah I know there's a billion of them). Actually, the counting works fine. I just need to get it write the count to a text file so that each line of vowels has the same number of a specific vowel as appears in the entire input file. For example, if the the letter E appears in the input file 5 times, then the line in the output file with the E's will look like EEEEE. It should write similar lines for each of the other vowels after the count. I'm trying to write a function to do that part, but I'm having trouble getting it off the ground.
I'd appreciate any input.
char ch,vBuff; /*A,E,I,O,U,*/
printf("\nCannot open input file");
printf("\nCannot open output file.txt");
if(ch=='A' || ch=='a')
if(ch=='E' || ch=='e')
if(ch=='I' || ch=='i')
if(ch=='O' || ch=='o')
if(ch=='U' || ch=='u')
printf("\nThere are a total of %d vowels in the input file.\n", vCount);
printf("\nVowel distribution is as follows:\n");
printf("\nA = %d, E = %d, I = %d, O = %d, U = %d\n",aCount,eCount,iCount,oCount,uCount);
January 21st, 2014, 10:06 AM
Your error tests for file opening exceeds what most other new programmers have done. The error messages could explicitly name the file that's missing.
I put 5 "a"s into data.txt. The program counted only 3. The trouble is that you read twice from the input stream each loop. fgetc and fscanf . So that's a problem.
With a ctypes function or macro you can change the case of the ch variable to a common case, shortening
Consider this option.
if ch is 'a' then it is not 'e'. Thus you could use "if then else". For common text the run time effect will be be miniscule since most characters will process through all the tests anyway. Also,
compiler optimization might rewrite the program for you.
After reading the input, the vowel counter will be the sum of the individual vowel counts. Instead of incrementing vCount in 5 places you could express vCount = aCount + eCount + ... + uCount; . This changes an O(n) operation to O(1) and that's a big victory.
Finally, on output:
for (i=0; i<aCount; ++i) fputc('A', fpout);
Except that since this code would be nearly duplicated 5 times, and we abhor code duplication, encapsulate that output business in a function. I'd pass as arguments the character, the tally of copies, and the pointer to file.
Last edited by b49P23TIvg; January 21st, 2014 at 10:08 AM.
[/code] are essential for python code and Makefiles!
January 21st, 2014, 08:43 PM
Thanks for the input @b49P23TIvg. Functions are still a bit dicey for me but it's high time I get a handle on how to use them properly. I'll definitely try your suggestions.