# Solved: Assign Numbers to Letters



## doctorzeus

Hey,

Question-what would be the easiest way to code a program in c/c++ that would output a number according to its neumeric postition in the alphabet without a lot of nested if statements e.g:

#include <iostream>

void main()
{

char letterin == 'A';

\\Stuff goes here that changes letterin into 1 (i.e. A = 1, B = 2, Z = 26)

}

Thanks

Doctorzeus


----------



## andythepandy

Hi, you could use the following method:



Code:


#include <iostream>
using namespace std;

char letters[27] = "abcdefghijklmnopqrstuvwxyz";
void main()
{
	while(true)
	{
		cout << "Enter a letter: ";
		char letterIn;
		cin >> letterIn;
		for(int i = 1; i <= 26; i++)
		{
			if(letterIn == letters[i-1])
			{
				cout << "Letter #" << i << "\r\n\r\n";
				break;
			}
		}
	}	
	
}

There I just have a char array with all the letters of the alphabet in, and a loop matches the letter entered by the user, and prints out the index+1 of the array, i.e the position of that letter in the alphabet.

Andy


----------



## calvin-c

You could also use the ASCII (or ANSI) equivalent. I'm not familiar enough with C/C++ to write the code, but VB has an ASC function that returns the decimal equivalent of a character. Then you'd need two checks since upper & lower case characters fall into different ranges. (The below assumes you've already checked to ensure x is a valid letter of the alphabet.)

If ASC(x) < 97 then ' lowercase a = Char(97) so x is uppercase, which starts at Char(41)
y = ASC(x) - 40
Else
y = ASC(x) - 96
End If


----------



## doctorzeus

andythepandy said:


> Hi, you could use the following method:
> 
> 
> 
> Code:
> 
> 
> #include <iostream>
> using namespace std;
> 
> char letters[27] = "abcdefghijklmnopqrstuvwxyz";
> void main()
> {
> while(true)
> {
> cout << "Enter a letter: ";
> char letterIn;
> cin >> letterIn;
> for(int i = 1; i <= 26; i++)
> {
> if(letterIn == letters[i-1])
> {
> cout << "Letter #" << i << "\r\n\r\n";
> break;
> }
> }
> }
> 
> }
> 
> There I just have a char array with all the letters of the alphabet in, and a loop matches the letter entered by the user, and prints out the index+1 of the array, i.e the position of that letter in the alphabet.
> 
> Andy


Thanks  very helpfull.

Doctorzeus


----------



## doctorzeus

calvin-c said:


> You could also use the ASCII (or ANSI) equivalent. I'm not familiar enough with C/C++ to write the code, but VB has an ASC function that returns the decimal equivalent of a character. Then you'd need two checks since upper & lower case characters fall into different ranges. (The below assumes you've already checked to ensure x is a valid letter of the alphabet.)
> 
> If ASC(x) < 97 then ' lowercase a = Char(97) so x is uppercase, which starts at Char(41)
> y = ASC(x) - 40
> Else
> y = ASC(x) - 96
> End If


Thanks I will look into this method too!

Looks like it will take up less resources and easier to write although calling functions like this in c/c++ is usually a lot more difficult 

Thanks

Doctorzeus


----------



## JimmySeal

You don't need to call any functions to use calvin-c's approach:



Code:


    char letterIn;
	int letterNum = 0;

	cout << "Enter a letter> ";
	cin >> letterIn;
	if(letterIn >= 'A' && letterIn <= 'Z')
	{
		letterNum = letterIn - 'A' + 1;
	}
	else if(letterIn >= 'a' && letterIn <= 'z')
	{
		letterNum = letterIn - 'a' + 1;
	}

	if(letterNum != 0)
	{
		cout << "Letter # " << letterNum;
	}
	else
	{
		cout << "Not a letter.";
	}


----------



## andythepandy

JimmySeal said:


> You don't need to call any functions to use calvin-c's approach:
> 
> 
> 
> Code:
> 
> 
> char letterIn;
> int letterNum = 0;
> 
> cout << "Enter a letter> ";
> cin >> letterIn;
> if(letterIn >= 'A' && letterIn <= 'Z')
> {
> letterNum = letterIn - 'A' + 1;
> }
> else if(letterIn > 'a' && letterIn <= 'z')
> {
> letterNum = letterIn - 'a' + 1;
> }
> 
> if(letterNum != 0)
> {
> cout << "Letter # " << letterNum;
> }
> else
> {
> cout << "Not a letter.";
> }


Yes that;s a good way to do it. You'll need to change
else if(letterIn > 'a' && letterIn <= 'z')
to
else if(letterIn >= 'a' && letterIn <= 'z')
otherwise it won't pick up a though


----------



## JimmySeal

Well spotted. Fixed it!


----------



## calvin-c

Umm, you realize that you actually are calling the function, right? All you've done is make the call implicit rather than explicit. There are arguments either way, of course, but I hate to see people fooling themselves by thinking that if they don't explicitly code it then it isn't happening.


----------



## doctorzeus

I refined it to create this version:



Code:


int assign_num(char letterIn)
{

    char letters[27] = "abcdefghijklmnopqrstuvwxyz";

    //Main part
    int letterOut = 0;

    for (int countn = 0; countn <= 27; ++countn)
    {
        if (letterIn == letters[countn])
        {
            letterOut = ++countn;
        }
    }
    return(letterOut);
}

I do a lot of C++ quizes where turning letters into their assigned number comes up VERY frequently so it can be used in various circumstances, just created a header file with it in so I can use it in the future and call the function which should be usefull 

Thanks

Doctorzeus


----------



## JimmySeal

calvin-c said:


> Umm, you realize that you actually are calling the function, right? All you've done is make the call implicit rather than explicit. There are arguments either way, of course, but I hate to see people fooling themselves by thinking that if they don't explicitly code it then it isn't happening.


I'm confused. Who's implicitly calling what function and who is fooling themself into thinking what's not happening?


----------



## calvin-c

letterIn - 'a' + 1 won't work unless you convert the character 'a' into its numeric equivalent-which requires calling a function. I'll note that the same function occurs when the string is assigned to a variable since it's stored in memory as its numeric equivalent-but that's what I mean about people fooling themselves into thinking that they aren't calling functions just because they don't call them explicitly.


----------



## JimmySeal

doctorzeus said:


> I refined it to create this version:
> 
> 
> 
> Code:
> 
> 
> omitted}
> 
> I do a lot of C++ quizes where turning letters into their assigned number comes up VERY frequently so it can be used in various circumstances, just created a header file with it in so I can use it in the future and call the function which should be usefull
> 
> Thanks
> 
> Doctorzeus


Not that it will impact your programs' performance terribly, but using a for loop to do this is quite silly and it doesn't even address capital letters. I would suggest using the approach that Calvin-c suggested and I demonstrated, as it is a lot more straightforward.


----------



## JimmySeal

calvin-c said:


> letterIn - 'a' + 1 won't work unless you convert the character 'a' into its numeric equivalent-which requires calling a function. I'll note that the same function occurs when the string is assigned to a variable since it's stored in memory as its numeric equivalent-but that's what I mean about people fooling themselves into thinking that they aren't calling functions just because they don't call them explicitly.


When I said that the code didn't need any functions, I was referring to doctorzeus' comment:



doctorzeus said:


> Looks like it will take up less resources and easier to write although calling functions like this in c/c++ is usually a lot more difficult


From that perspective, the code does, for all intents and purposes, not have any function calls so your nitpicking is quite irrelevant.

Aside from that, your comment isn't even correct. In C, and C-based languages, *char*s are numeric types that get special treatment in certain situations. The 'a' token would be converted to its equivalent numeric value at compile time, so there is no implicit function call at runtime to convert it to a number. And unlike VB, C/C++ don't even have functions for converting between chars and ints, though cast operators can be used to convert between them.

When I said your approach didn't need any functions, I was actually defending your comment against doctorzeus' notion that it was "a lot more difficult." I don't know what you're trying to get at, but I do suggest that you have some idea what you're talking about before making any more obnoxious, condescending remarks.


----------



## doctorzeus

JimmySeal said:


> When I said that the code didn't need any functions, I was referring to doctorzeus' comment:
> 
> From that perspective, the code does, for all intents and purposes, not have any function calls so your nitpicking is quite irrelevant.
> 
> Aside from that, your comment isn't even correct. In C, and C-based languages, *char*s are numeric types that get special treatment in certain situations. The 'a' token would be converted to its equivalent numeric value at compile time, so there is no implicit function call at runtime to convert it to a number. And unlike VB, C/C++ don't even have functions for converting between chars and ints, though cast operators can be used to convert between them.
> 
> When I said your approach didn't need any functions, I was actually defending your comment against doctorzeus' notion that it was "a lot more difficult." I don't know what you're trying to get at, but I do suggest that you have some idea what you're talking about before making any more obnozious, condescending remarks.


I meant functions in the included library headers...you know like writeprocessmemory() which took me a while to get my head round if you know what I mean..I know that I'm already calling a function 

Clearly I misspoke,

Doctorzeus


----------



## doctorzeus

JimmySeal said:


> Not that it will impact your programs' performance terribly, but using a for loop to do this is quite silly and it doesn't even address capital letters. I would suggest using the approach that Calvin-c suggested and I demonstrated, as it is a lot more straightforward.


Resources not really a concern for the programs I'm asked to build in these, the quizes are really to test logic.

but like I said I'm trying that method as well


----------



## calvin-c

User logic or logic circuits? Note that Jimmy's and my approaches differ mostly in how easily a programmer can understand what they do. I learned my programming in strongly-typed languages with little implicit conversion capability-that's why I prefer explicit conversions over implicit. These days, with object-oriented programming, it doesn't make all that much difference. Put the function into a library (or header file) and the programmer that uses it really doesn't need to know how it works, only what it does.


----------



## doctorzeus

calvin-c said:


> User logic or logic circuits? Note that Jimmy's and my approaches differ mostly in how easily a programmer can understand what they do. I learned my programming in strongly-typed languages with little implicit conversion capability-that's why I prefer explicit conversions over implicit. These days, with object-oriented programming, it doesn't make all that much difference. Put the function into a library (or header file) and the programmer that uses it really doesn't need to know how it works, only what it does.


User Logic...I know it's not essential that u don't know exacly how they work but since I switched from VB to C/C++ I've always liked the fact that you can open up all the header files to see exacly how they work and understand how they work :up:.

Thanks for the help, I'm trying your method as well.

Doctorzeus


----------



## JimmySeal

doctorzeus said:


> I meant functions in the included library headers...you know like writeprocessmemory() which took me a while to get my head round if you know what I mean..I know that I'm already calling a function
> 
> Clearly I misspoke,
> 
> Doctorzeus


Just so that there's no confusion, that post that you're quoting was directed at calvin-c, not you.


----------



## doctorzeus

Yeah I know,

It just seemed to be causing some heated internal dispute and was trying to clear up the issue


----------



## calvin-c

Heated internal dispute? Not on my part.


----------



## nj2b

doctorzeus said:


> Hey,
> 
> Question-what would be the easiest way to code a program in c/c++ that would output a number according to its neumeric postition in the alphabet without a lot of nested if statements e.g:
> 
> Thanks
> 
> Doctorzeus


 What about a "one liner" using something like


Code:


    char letters[] = "abcdefghijklmnopqrstuvwxyz", entered = 'J';    printf("%d", strchr(letters, tolower(entered)) != NULL ? (strchr(letters, tolower(entered)) - letters) + 1 : -1);


----------

