Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:40:19+00:00 2026-06-14T02:40:19+00:00

This code really confuses me, it is using some Stanford libraries for the Vector

  • 0

This code really confuses me, it is using some Stanford libraries for the Vector (array) class. Can anyone tell me what is the purpose of int index = line [j] - 'a'; why – ‘a’?

void countLetters(string filename)
{
Vector<int> result;

ifstream in2;
in2.open(filename.c_str());
if (in.fail()) Error("Couldn't read '" + filename + "'");

for (int i = 0; i < ALPHABETH_SIZE; i++)
{
    result.add(0);  // Must initialize contents of array
}

string line;
while (true)
{
    getLine(in, line);
    // Check that we got a line
    if (in.fail()) break;

    line = ConvertToLowerCase(line);
    for (int j = 0; j < line.length(); j++)
    {
        int index = line [j] - 'a';
        if (index >= 0 && index < ALPHABETH_SIZE)
        {
            int prevTotal = result[index];
            result[index] = prevTotal +1;
        }
    }
}
}

The purpose of the code:

Takes a filename and prints the number of times each letter of the alphabet appears in that file. Because there are 26 numbers to be printed, CountLetters needs to create a Vector. For example, if the file is:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:40:20+00:00Added an answer on June 14, 2026 at 2:40 am

    Characters in a string are encoded using a character set… typically ASCII on hardware common in English language systems. You can see the ASCII table at http://en.wikipedia.org/wiki/ASCII

    In ASCII (and most other character sets), the numbers representing letters are contiguous. So, this is the natural way to test whether the character at index j in character-array line is a letter:

    line[j] >= 'a' && line[j] <= 'z'
    

    Your program is equivalent to that, in an algebra-kind of sense it subtracts a from both sides (knowing that a is the first character in the character set):

    line[j] >= 'a' - `a` && line[j] <= 'z' - `a`
    
    line[j] >= 0 && line[j] <= 'z' - `a`
    

    Replacing “<= z – a” with am equivalent:

    line[j] >= 0 && line[j] < ALPHABET_SIZE
    

    where ALPHABET_SIZE is 26. This trades a dependency on knowing z is the last character of your character set for knowing how many characters are in your character set – both are a little fragile, but fine if you know you’re dealing with a well-known, stable character set encoding.

    A better way to check for a letter is to use the isalpha() predicate: http://www.cplusplus.com/reference/clibrary/cctype/isalpha/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code returns an error: AttributeError: can't set attribute This is really a pity
Im running into this error that I can't work out Im writing some code
I really thought I understood Python variable referencing, so I'm confused why this code
Editing this code here on Stackoverflow and I'm really near to get the result
A really weird thing happens in this code JNIEXPORT jintArray JNICALL Java_jsdr_SdrLibrary_getTunerGains (JNIEnv *
I'm not really getting how this code does what it does: char shellcode[] =
I really do not understand how is this error happening at this code. Please
this seems to be weird but it really happened. I have this code working
If there is any possibility to make this code simpler, I'd really appreciate it!
I need to get the maximum value out of this code, but it's really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.