0

Reading and Counting number of upper case and lower case alphabets from a exsisting text file and printing it .c++ programme .

#include<iostream> #include<fstream> #include<conio.h> #include<ctype.h> #include<string.h> using namespace std; int count () { char ch; int upper=0,lower=0; ifstream fl("NewFile.txt"); while (!fl.eof() ) { fl.get (ch) ; if (isupper(ch) ) upper++; if (islower(ch) ) lower++;} cout <<" no. of uppercase alphabets= " << upper <<endl; cout <<" no. of lowercase alphabets= " << lower <<endl; } int main() { count(); getch(); return 0;} please help someone !! Fast

12th May 2019, 12:30 PM
D Daniel
D Daniel - avatar
2 Réponses
0
my code is showing either the upper or the lower case correct and the opposite of it giving the output of total number of alphabets plus one . someone please help i can not figure it out
12th May 2019, 12:32 PM
D Daniel
D Daniel - avatar
0
eof() checks return value from last call of get(ch) (it should be called once more after it gets the last char from your file). So in your code last char passes while loop twice. In order to fix it try to get first char before while loop, then inside the loop check its case and finally read new char. Like this: fl.get(ch); while (!fl.eof()) { if () {} if () {} fl.get(ch); }
13th May 2019, 5:09 AM
pascal
pascal - avatar