#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Printing repeating characters between text files


    Hey everyone,

    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.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main()
    {	
    	FILE *fpin;
    	FILE *fpout;
    	
    	char ch,vBuff[500];  /*A[100],E[100],I[100],O[100],U[100],*/
    	int aCount=0,eCount=0,iCount=0,oCount=0,uCount=0,vCount=0;
       
    	if((fpin=fopen("data.txt","r"))==NULL)
    	{
    		printf("\nCannot open input file");
    		exit(1);
    	}
    	if((fpout=fopen("fileOUT.txt","a"))==NULL)
    	{
    		printf("\nCannot open output file.txt");
    		exit(2);				
    	}
    	
    	while((ch=fgetc(fpin))!=EOF)										
    	{
    		fscanf(fpin,"%c",	vBuff);	
    		if(ch=='A' || ch=='a')		
    			{aCount++;
    			 vCount++;}
    		if(ch=='E' || ch=='e')
    			{eCount++;
    			 vCount++;}
    		if(ch=='I' || ch=='i')
    			{iCount++;
    			 vCount++;}
    		if(ch=='O' || ch=='o')
    			{oCount++;
    			 vCount++;}
    		if(ch=='U' || ch=='u')
    			{uCount++;
    			 vCount++;}
    		
    	}
    	
    	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);
    	
    	
    	fclose(fpin);
    	fclose(fpout);
    	
    	return 0;
    }

  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,891
    Rep Power
    481
    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
    if (('e'==ch)||('E'==ch))
    to
    if ('e'==ch)
    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:

    int i;
    for (i=0; i<aCount; ++i) fputc('A', fpout);
    fputc('\n', 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]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo