Hi - I need help in how to count the number of lowercase characters while reading input from a text file in C++.
Thanks.
8 replies to this topic
#1
Posted 01 August 2010 - 02:59 PM
|
|
|
#2
Posted 01 August 2010 - 03:07 PM
This is a portion of what I have attempted so far:
count=0;
indata >> a;
while ( !indata.eof() ) {
indata >> a;
cout<<a;
count = count + 1;
}
#3
Posted 01 August 2010 - 03:08 PM
Read the text file and store its content in a char array. Then do this:
The text file should consist of letters.
Greets,
artificial
char cBuffer[256];
int nNumberOfLowercaseLetters = 0;
for(int a = 0; a < 256; a++)
{
if(cBuffer[a] > 'Z')
nNumberOfLowercaseLetters++;
}
The text file should consist of letters.
Greets,
artificial
Sometimes words ain't enough to express something. That's why computer scientists use double words.
#4
Posted 01 August 2010 - 03:14 PM
Great! What is cBuffer[256]? How would I store the contents in a character array?
#5
Posted 01 August 2010 - 03:22 PM
skastu01 said:
Great! What is cBuffer[256]? How would I store the contents in a character array?
cBuffer is the char array that stores the text file's content.
Take a look at this: Link
Greets,
artificial
Sometimes words ain't enough to express something. That's why computer scientists use double words.
#6
Posted 01 August 2010 - 04:22 PM
artificial said:
Read the text file and store its content in a char array. Then do this:
The text file should consist of letters.
Greets,
artificial
char cBuffer[256];
int nNumberOfLowercaseLetters = 0;
for(int a = 0; a < 256; a++)
{
if(cBuffer[a] > 'Z')
nNumberOfLowercaseLetters++;
}
The text file should consist of letters.
Greets,
artificial
I've never liked making assumptions over what does or does not exist in a text file. If using your paradigm above wouldn't:
char cBuffer[256];
int nNumberOfLowercaseLetters = 0;
for(int a = 0; a < 256; a++)
{
if(cBuffer[a] >= 'a' && cBuffer[a] <= 'z' )
nNumberOfLowercaseLetters++;
}
be a bit safer?
Normally though, i'd just use the islower() function out of ctype.h in the conditional.
#7
Posted 01 August 2010 - 05:16 PM
d0s said:
I've never liked making assumptions over what does or does not exist in a text file.
You're right, but I wanted to show him the easiest solution. :)
Greets,
artificial
Sometimes words ain't enough to express something. That's why computer scientists use double words.
#8
Posted 03 August 2010 - 07:55 PM
Thank you for your assistance! I have successfully completed the program, and this is a"her".
#9
Posted 04 August 2010 - 05:03 AM
Blargh, wrote a sample before I read the thread trough.
So, heres how I might have done it. ( not tested )
So, heres how I might have done it. ( not tested )
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
template < typename Char >
int lowerCount( const std::basic_string<Char>& str )
{
int result = 0;
std::for_each( str.begin(), str.end(), []( Char c )
{
result += std::islower( c );
});
return result;
}
int main( int argc, char* argv[] )
{
std::fstream file( "some_file.txt" );
int fileSize = 0;
std::string fileContent;
if ( !file.is_open() )
{
// failed to open file
return 0;
} // if
file.seekg( 0, std::ios::end );
fileSize = file.tellg();
file.seekg( 0, std::ios::beg );
fileContent.resize( fileSize );
file.read( &fileContent[0], fileSize );
file.close();
std::cout << "lower count=" << lowerCount( fileContent ) << std::endl;
return 0;
}
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









