Jump to content

Count of Lowercase Letters

- - - - -

  • Please log in to reply
8 replies to this topic

#1
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
Hi - I need help in how to count the number of lowercase characters while reading input from a text file in C++.

Thanks.

#2
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
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
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts
Read the text file and store its content in a char array. Then do this:


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
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
Great! What is cBuffer[256]? How would I store the contents in a character array?

#5
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts

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
d0s

d0s

    Newbie

  • Members
  • PipPip
  • 10 posts

artificial said:

Read the text file and store its content in a char array. Then do this:


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
artificial

artificial

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 624 posts

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
skastu01

skastu01

    Newbie

  • Members
  • PipPip
  • 27 posts
Thank you for your assistance! I have successfully completed the program, and this is a"her".

#9
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
Blargh, wrote a sample before I read the thread trough.
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