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 6908431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:33:45+00:00 2026-05-27T08:33:45+00:00

So I thought I’d teach myself C++ but I appear to have no problems

  • 0

So I thought I’d teach myself C++ but I appear to have no problems with the language but I am frankly stupid.

So my idea was this. If I say a=1, b=2, z=26, aa=27 etc, I can map words to numbers, use a boolean in a hash table (bit masked of course) and have an O(1) spell checker. So writing that is no problem at all. My algorithm to do it, looks like this:

int pos;
word_key_t char_key;
word_key_t key = 0;
const char *raw = word.c_str();

cout << "Entering getKey loop with " << raw << endl;
for (pos = 0; raw[pos] != '\0'; pos++) {
    if (raw[pos] >= 'A' && raw[pos] <= 'Z') {
        char_key = raw[pos] - 'A';
    } else if (raw[pos] >= 'a' && raw[pos] <= 'z') {
        char_key = raw[pos] - 'a';
    } else {
        throw new runtime_error("Unrecognised Character");
    }

    key += (char_key + 1) * (pow(CHARS_IN_ALPHABET, pos));
}

cout << "word: " << raw << " ,score: " << key << endl;
return key;

It appears to work,

a=1
b=2
ab=53
ac=79.

I believe this is correct.

However, I’m having problems trying to decode it. This is my best attempt and it isn’t working. I believe I need to be using pow(26,position) and decrementing from the end of the string but I’m just struggling to achieve this. This is some workable standalone code that does the wrong thing:

#include <iostream>
#include <inttypes.h>
#include <string.h>
 
typedef uint32_t word_key_t;
const int CHARS_IN_ALPHABET = 26;
const int BUFFER_SIZE = 255; //ignore this for now.
 
using namespace std;
 
string reverseKey(const word_key_t key); //broken algo
 
int main(int argc, char** argv) {
        reverseKey(53); // 53 = ab
        return 0;
}
 
//disassemble a word_key_t into it's original string. returns lowercase only
string reverseKey(const word_key_t key)
{
   
  char chr, buffer[BUFFER_SIZE];
  word_key_t keyc = key, isolated, pos = BUFFER_SIZE;
 
  cout << "key: " << keyc << endl;
  
  while (keyc != 0) {
      isolated = (keyc - 1) % ((word_key_t)CHARS_IN_ALPHABET + 1);
      cout << "key: " << keyc << ", isolated: " << isolated << endl;
      chr = (char)'a' + isolated - 1;
      cout << "isolated character: " << chr << endl;
      keyc = (keyc - isolated) / CHARS_IN_ALPHABET;
      cout << "new key: " << keyc << endl;
      pos++;
  }
 
  string s("test");
  return s;
 
}

If someone could nudge me toward the correct psudeocode for solving this I’d be really grateful. I have kind of gone a bit crazy and lost the plot with the solution. I just can’t see it. Something is telling me to 2logX / 2log26 and I think I just need some smarter eyes on it. Then I can get back to learning C++.

  • 1 1 Answer
  • 2 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-05-27T08:33:45+00:00Added an answer on May 27, 2026 at 8:33 am

    Some editing later. I did misunderstand the generation of the key values. I think the generation of the letters from the key would be:

    while ( key )
       {
       int char_key = key % 26;
       char c = 'a' + (char)( char_key - 1 );
       key /= CHARS_IN_ALPHABET;      
       }
    

    Although I still don’t think the original calculation of the key is correct. I still believe the key computation should be:

    key = CHARS_IN_ALPHABET * key + char_key + 1;
    

    Process the raw[] array in reverse order to avoid extracting them in reverse order.

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

Sidebar

Related Questions

I thought this would be fairly easy, but I'm totally baffled. I want one
Never thought I'd have this problem :) The following snippet of code works in
I thought this would be really easy but I can't find a simple solution,
Thought this might work: ${TM_FILENAME/(.+?)(\.[^.]*$|$)/} But it's giving me this error: Error Parsing Snippet
I thought this would be pretty academic but its not. I'm trying to traverse
I thought this would be fairly trivial but it's turned out be more difficult
I thought this would be easy, but after scanning many pages Google hasn't come
I thought this would be simpler to verify, but I haven't been in VS
I thought I had this figured out, but can't get it working correctly: All
Thought is was time to bring this to the Stack. We have IIS 7.5

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.