Hi guys i am trying to write a program that counts the number of lowercase characters in an input file and display the two most used lowercase letters. My problem is I dont know how to get my program to count each lowercase character and save them into an array of size 26 using an if loop, but im having trouble getting it to work.

#include <iostream>
#include<fstream>
#include <cstdlib>

using namespace std;

int main()
{
   const int FIRST_LOWERCASE= 97;
   const int LAST_LOWERCASE= 122;
   const int SIZE= (LAST_LOWERCASE-FIRST_LOWERCASE);
   char letter;
   int array[SIZE];
   string str;

   ifstream inputFile("INPUT.txt"); // Opening input file
        if(!inputFile){
            cout<< "ERROR... the file INPUT.txt could not open.\n";
            exit(1);
        }

     while(getline(inputFile,str)){// Reading from file and displaying characters
        inputFile >> str;
        cout << str; // Displaying to check will be deleted later on
     }
        if(letter<=FIRST_LOWERCASE&& letter>= LAST_LOWERCASE){// if loop to check for case of character

        }

    inputFile.close();// Closing file
}

Thanks in advance for your help.

Recommended Answers

All 7 Replies

If you intend to skip whitespaces I suggest you read using operator>>
In your original code you have both:

getline(inFile,str);
//and
inFile >> str;

Perhaps re-writing your loop condition to:

while(inFile >> str)
{

}

and the operator '>>' will read in one block of non-whitespace characters, or a "word" at a time.

I would suggest you read the file one line at a time using getline. After you read in the line pass it to a function that will go through the string and check for lowercase letters. Here is a way to get a counter for the lowercase letters

int main()
{
    int lowercase[26];
    for (int i = 0; i < 26; i++)
        lowercase[i] = 0;
    // check string for lowercase letters
    if (letter == lowercase letter)
        lowercase[letter - 97] += 1;
}

if you incluse <cctype> library you can use its functions like isupper() and islower()

Crutoy that looks like it would be easier but we haven't learned those functions, so I probably shouldnt use them, but thanks for the suggestion.

have you come up with the rest of the logic ?

This is what I have so far. But I don't know how right it is or if it will work in the end

char Letter;
    while(inputFile>>str){
        for(int i=0;i<str.length();i++){
            Letter=char(lowercase[i]);
                if(Letter>= FIRST_LOWERCASE && Letter<= LAST_LOWERCASE){
                    lowerCaseCount[(Letter)-FIRST_LOWERCASE]++;
                }
            }
    }

I figured it out, thanks everyone for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.