Question

I'm currently doing a project where I need to create two data structures that will be used to contain strings. One of them has to be a form of linked list, and I've been adivsed to seperate the words out into seperate lists inside of it for each letter of the alphabet. I'm required to think about efficiency so I have an array of Head pointers of size 26, and am wanting to convert the first character of the word given into an integer so I can put it into the subscript, such as:

//a string called s is passed as a parameter to the function
int i = /*some magic happens here*/ s.substr(0, 1);
currentPointer = heads[i]; //then I start iterating through the list

I've been searching around and all I've seemd to have found is how to covert number characters that are in strings into integers, and not letter characters, and am wondering how on earth I can get this working without resorting to a huge and ugly set of if statements

Was it helpful?

Solution

When you are setting i to the value of the first character, you are getting the ASCII value. So i is out of your 0-25 range : See man ascii You can reduce it by substraction of the first alaphabet ascii letter. (Be care full with the case)

 std::string   s("zoro");
 int   i = s[0];

 std::cout << "Ascii value : " << i << " Reduced : " << i - 'a' << std::endl;

Which produce the ASCII value 'z' = 112 and 25 for the reduced value, as expected.

OTHER TIPS

I think you are confusing values with representations. "Ten" "10" and "1 1 1 1 1 1 1 1 1 1" are all the same value, just represented differently.

I've been searching around and all I've seemd to have found is how to covert number characters that are in strings into integers, and not letter characters

There's no difference. Characters are always represented by integers anyway. It's just a matter of representation. Just present the value the way you want.

By the way, this is a key concept programmers have to understand. So it's worth spending some time thinking about it.

A classic example of this misunderstanding is a question like "I have a variable i that has some value in decimal. How can I make it store a value in hex?" Of course that makes no sense, it stores values and hex and decimal are representations. If you have ten cars, you have ten in cars, not in decimal or hex. If i has the value ten, then the value ten is in i, not a representation of ten in decimal or hex.

Of course, when you display the value stored in i, you have to choose how to represent it. You can display it as ten, or 10, | | | | | | | | | | |, or whatever.

And you might have a string that has a representation of the value "ten" in hex, and you might need to assign that value to a variable. That requires converting from a representation to the value it represents.

There are input and output functions that input and out values in various representations.

I suspect you want to convert digits stored in strings as characters to integers, e.g. character '9' to integer 9.

In order to do so:

char c = '9';
int x = c - '0';

That will work regardless of whether you have a computer using ASCII or EBCDIC...

In this case, you don't seem to need atoi or itoa (neither is going to do anything very useful with, for example, J). You just want something like:

int i = tolower(s[0])-'a';

In theory that's not portable -- if there's any chance of the code being used on a machine that uses EBCDIC (i.e., an IBM or compatible mainframe) you'll want to use something like 'z'-'a' as the size of your array, since it won't be exactly 26 (EBCDIC includes some other characters inserted between some letters, so the letters are in order but not contiguous).

Probably more importantly, if you want to support languages other than English, things change entirely in a hurry -- you might have a different number of letters than 26, they might not all be contiguous, etc. For such a case your basic design is really the problem. Rather than fixing that one line of code, you probably need to redesign almost completely.

As an aside, however, there's a pretty good chance that a linked list isn't a very good choice here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top