determining number placement of a letter in alphabet with suffix

Oct 25, 2014 at 4:14am
Hey anyone that can help would be greatly appreciated

I am writing a code to output the number placement of a letter in the alphabet. I already understand how to find the number placement by minus 64 for lowercase and 96 for upper. What I need help with is figuring out how to add the suffix for it (st, nd, rd,th), such as 1st, 2nd, ect.
Oct 25, 2014 at 4:24am
Hi,

Welcome to cplusplus :+)

What code do you have already? When you do post code, make sure you use code tags with the <> button on the format menu

Have you considered using a switch statement? If so look it up in the reference section at the top left of this page. There is also the tutorial & articles.

Hope all is well at your end.
Oct 25, 2014 at 4:50am
hey thanks for the reply, I have a lot of code because my homework consists of a lot other than this but ill post the section im on now and if you need more ill post it.

1
2
3
4
5
6
7
8
9
10
11
//output character place in alphabet if a letter
	if (FavChar >= 65 && FavChar <= 90)
	{
		NumPlace = FavChar - 64;
	}
	else if (FavChar >= 97 && FavChar <= 122)
	{
		NumPlace = FavChar - 96;
	}

	cout << "\n\tand it is number " << NumPlace << "in the alphabet";
Oct 25, 2014 at 5:02am
Your very right about the switch statement, if I mod by 10 then I can get the last digit and apply the correct suffix, the only problem is how to deal with 1st vs 11th, 2nd vs 12th,and 3rd vs 13th
Oct 25, 2014 at 5:03am
Perhaps this will be helpful: http://forums.devshed.com/programming-42/function-returns-suffix-594630.html
They talk about adding suffixes and show c++ code. :)
Oct 25, 2014 at 5:04am
Hi,

So you can find out what the last numeral is by using the modulus operator %

Then use a switch to decide what to do for each one.

Did you look in the reference for how a switch works?
Oct 25, 2014 at 5:44am
thanks a lot guys I got it figure out. I switched to else if statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//calculate character place in alphabet if a letter
	if (FavChar >= 65 && FavChar <= 90)
	{
		NumPlace = FavChar - 64;
	}
	else if (FavChar >= 97 && FavChar <= 122)
	{
		NumPlace = FavChar - 96;
	}

	//add suffix to alphabet number place
	if (NumPlace % 10 == 11 || NumPlace % 10 == 12 || NumPlace % 10 == 13)
	{
		NumSuffix = "th";
	}
	else if (NumPlace % 10 == 1)
	{
		NumSuffix = "st";
	}
	else if (NumPlace % 10 == 2)
	{
		NumSuffix = "nd";
	}
	else if (NumPlace % 10 == 3)
	{
		NumSuffix = "rd";
	}
	else
	{
		NumSuffix = "th";
	}
	cout << "\n\tand it is number " << NumPlace << NumSuffix << " letter in the alphabet";
Oct 25, 2014 at 5:50am
Hi,


NumPlace % 10

gives the remainder when divided by 10, so it will never be 11, 12, or 13
Oct 25, 2014 at 5:59am
right I just found that error and fixed it.

Thanks again for the help, it is much appreciated.
Oct 25, 2014 at 6:13am
No worries :+)
Topic archived. No new replies allowed.