Thread: converting to CAPS from a text file

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    converting to CAPS from a text file

    Hey guys,

    I'm trying to find a way to count all the alphabetic (letters) in a certain text file. Then, convert them to uppercase and display the number of times that letter appears in that text. I'm attaching the text file as it is hard coded.

    here is what I have so far:

    Code:
    const int LIMIT = 128;
    
    	ifstream inStream;
    	
                    int lettersArray[LIMIT];
    	int i;
                    char c;
    
    	inStream.open("darwin.txt");
    
    	if( inStream.fail() )
    	{
    		cout << "The input file, failed to open. Exiting....\n\n";
    		exit(-1);
    	}
    
    	for ( i=0; i<LIMIT; i++ )
    	{
    		lettersArray[i] = 0;
    	}
    
    	cout << "The input file contains: \n";
    	
    	while( inStream >> c)
    	{
    		lettersArray[c]++;
    	}
    
    	cout << endl;
    
    	cout << "The non-zero table of values for printable characters are: \n";
    
    	for( i=0; i<LIMIT; i++ )
    	{
    		if( isprint(i) && (lettersArray[i] !=0) )
    		{
    			cout << (char)i << ":" << lettersArray[i] << ";" ;
    		}
    		cout << endl;
    	}
    }
    this unfortunately counts and shows ALL the characters in the text. I'm not sure if there is a function that selects only the letters, but I know I have to us 'toupper()' somewhere.

    any help?

    axon
    Last edited by axon; 03-03-2003 at 06:16 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Thanks Salem! I figured out the CAPS conversion, but couldn't get the alphatical characters...now it works fine!

    thanks a lot!

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Salem or anyone else: I made the code with Salem's help, but I don't think I understand everything fully. How does the program exactly count all the instatnces that a certain letter appears? Where exactly in my code does this happen.

    I get this a lot...I write my code by trial and error and then I don't know what is going on. any suggestions? explanations?

    axon

    here is the complete code again
    Code:
    	const int LIMIT = 128;
    	int sum;
    
    	ifstream inStream;
    	int lettersArray[LIMIT];
    	char c;
    	int i;
    
    	inStream.open("darwin.txt");
    
    	if( inStream.fail() )
    	{
    		cout << "The input file, failed to open. Exiting....\n\n";
    		exit(-1);
    	}
    
    	for ( i=0; i<LIMIT; i++ )
    	{
    		lettersArray[i] = 0;
    	}
    
    	cout << "The input file contains: \n";
    	
    	while( inStream >> c)
    	{
    		lettersArray[toupper(c)]++;
    		//cout << c << " ";
    	}
    
    	cout << endl;
    
    	cout << "The table of alphabetic characters is: \n";
    
    	for( i='A'; i<='Z'; i++ )
    	{
    			cout << (char)i << ":" << lettersArray[i] << ";" ;
    			cout << endl;
    	}
    	cout << endl;

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM