Thread: Text file

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Text file

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main()
    {
    	FILE *fptr;
    	char cha, vowel[11] = {"AEIOUaeiou"};
    	int i, j, n_char = 0, n_let = 0, n_vow = 0, n_con = 0, letter[26] = {0};
    
    
    	for (i=0; i<26; i++)
    	{
    		letter[i] += 65 + i;
    	}
    
    
    	fptr = fopen("text.txt","r");
    
    
    	while ( !feof(fptr) )
    	{
    		fscanf(fptr,"%c",&cha);
    		printf("%c", cha);
    			n_char++;
    
    
    		for (i=0; i<26; i++)
    		{
    			if ( cha == (char)letter[i] || cha == tolower( (char)letter[i]) )
    			{
    				n_let++;
    				for (j=0; j<10; j++)
    				{
    					if ( cha == vowel[j] )
    						n_vow++;
    				}
    			}
    		}
    
    
    		
    		if ( !(cha == 'a' ||cha == 'e'|| cha == 'i'|| cha == 'o'||cha == 'u') )
    			n_con++;
    	
    		
    	}
    	printf("\nTotal number of characters: %d\n", n_char);
    	printf("Number of letters: %d\n", n_let);
    	printf("Number of vowels: %d\n", n_vow);
    	printf("Number of consonants: %d\n", n_con);
    }
    Text file-c-jpg
    How to avoid the repetition of last character?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't use feof to control the loop in that way. Instead use the return value of fscanf, e.g.,
    Code:
    while ( fscanf(fptr,"%c",&cha) == 1 )
    The reason is that the feof call only returns a true value when the end of file condition is detected, but that condition happens only after an attempt is made to read past the end of file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Why number of consonants cannot get 54?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You don't need to do any comparisons to count n_con. Just do n_con = n_let - n_vow;
    The reason you're getting a number much higher than expected is because you are counting all letters, spaces, numbers, capitals that are not one of 'a', 'e', 'i', 'o', 'u'.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    I know can use n_con = n_let - n_vow; to do. If I dun wan use tat method, is there another solution to solve it?

    *How to filter the spaces?
    Last edited by ulti-killer; 10-21-2012 at 03:04 AM.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Just count the stuff you want to count.
    Something like:
    Code:
    while there is a character
        number_of_characters++
        if character is a letter
            number_of_letters++
            if character is a vowel
                number_of_vowels++
            else
                number_of_consonants++
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 11-11-2011, 10:32 PM
  2. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  3. Replies: 8
    Last Post: 05-05-2010, 02:43 PM
  4. Replies: 1
    Last Post: 10-30-2002, 05:45 AM
  5. Replies: 4
    Last Post: 06-22-2002, 12:23 PM