ok - challenge number 2: What now if i would like to input letters and the letters would be displayed as numbers?
For instace i would like to input b a d and it would display me the corresponding numbers. The opposite of what Mr.Dave Sinkula (look thread:Need help with integers in c++) did in the previous thread...Moreover, what if i would like to assign my own numbers to the letters...?
For those who see this thread first time, please look first at thread "Need help with integers in c++"

--Thanx in advance--

Dani AI

Generated

A short, practical guide to map letters to numbers (custom or computed). — there are two common approaches: a dense table (array indexed by the character code) which is very fast, or an associative container (std::map / std::unordered_map) which is simpler when your set of letters is sparse or you want easy initialization. 's 256-slot idea is fine; combine it with a safe cast to avoid negative indexes. 's pair/struct approach is fine too — a map gives the same result with less lookup code.

Array (fast, works for plain ASCII):

int nums[256] = {0};                 // all zero by default
nums[(unsigned char)'a'] = 10;
nums[(unsigned char)'b'] = 25;
nums[(unsigned char)'d'] = 40;

char ch = 'b';
int value = nums[(unsigned char)ch]; // safe lookup

Map (clear, flexible, good for custom mapping):

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
  std::unordered_map<char,int> m;
  m['a'] = 10; m['b'] = 25; m['d'] = 40;
  std::string token;
  while (std::cin >> token) {
    for (char c : token) {
      auto it = m.find(c);
      std::cout << (it != m.end() ? std::to_string(it->second) : "?") << ' ';
    }
    std::cout << '\n';
  }
}

Notes and gotchas: static const char alphabet[] = "a=10, b=25, c=30"; is just a string literal — not a mapping — so you must store pairs (array or map). Always cast to unsigned char when using a char as an array index (some platforms have signed char). Normalize case if needed (use tolower) and decide whether you want to treat spaces as separators. If input may be UTF-8 or wide characters, you need proper Unicode handling (decode to code points or use wide strings) rather than indexing a 256-table. For reverse lookup, build a second map or a vector indexed by the number. Finally, debug by printing numeric codes (static_cast<int>(static_cast<unsigned char>(ch))) to verify what the program actually sees.

Recommended Answers

All 6 Replies

anyone, please?

Why are you so impatient?

Make an array of integers of length 256. Maybe named 'nums'. Use the character as the index; to get the number for 'x', you'd write nums. You'll need to initialize the values of the array of course.

And pray that your characters are eight bits. This method does not scale too well for larger character sizes. If your characters are nine bits wide, you'll need an array of integers of length 512. If 16 bits wide, 65536. So stick with 256 and just check to make sure your character is less than 256.

How a correct initialazation of arrays would be correct?
static const char alphabet[] = "a=10, b=25, c=30"; //and so on
i get a compiler error when i initialize like this...

anyway - by trial and error i hope i find the answer
thanx for ur time Rashakil

Initialize arrays like this:

int some_primes[9] = {2, 3, 5, 7, 11, 13, 17, 19, 23};

How a correct initialazation of arrays would be correct?
static const char alphabet[] = "a=10, b=25, c=30"; //and so on
i get a compiler error when i initialize like this...

You can do it like this:

#include <iostream>
struct alphab
{
    int num;
    char ch;
};

int main()
{
    alphab al[] = {{1,'a'},{2,'b'},{3,'c'}}; // etc.
    int n = 1; // testing
    std::cout << al[n].ch << std::endl; //shall print out b.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.