We help IT Professionals succeed at work.

Letters to numbers C#

kyrenator
kyrenator asked
on
Medium Priority
614 Views
Last Modified: 2012-05-06
How can i make a program in c# that it will take from a textbox a word e.x. "George" and it will make it a number like this : G=7, e=5, o=15, r=18, g=7, e=5 then the number of the word George is G+e+o+r+g+e = 7+5+15+18+7+5 = 57????
Comment
Watch Question

anarki_jimbelSenior Developer
CERTIFIED EXPERT

Commented:
I reckon you want to use alphabet indeces as numbers for letter, am I right?

Really it is quite easy implement, e.g. by putting all the alphabet into a list ot char array.

Do you need some sample code?

Author

Commented:
yes if it is easy for you!

Commented:
See code snippet
 

string inputField = "George";
 
System.Text.Encoding ascii = System.Text.Encoding.ASCII; 
Byte[] encodedBytes = ascii.GetBytes(inputField); 
 
// Initialize AsciiSum Field
int AsciiSum = 0;
 
foreach (Byte b in encodedBytes) 
{
	if ((b>64) && (b<91))
	{
		// The Letter A is ASCII 65 and the Letter Z is ASCII 90
		// subtract 64 to get the value we need to add to our ascii sum
		AsciiSum += b-64;
	}
	else
	{
		if ((b>96) && (b<123))
		{
			// The Letter a is ASCII 97 and the Letter z is ASCII 122
			// subtract 96 to get the value we need to add to our ascii sum
			AsciiSum += b-96;
		}
	}
}
MessageBox.Show(AsciiSum.ToString());

Open in new window

Author

Commented:
and how can i connect this with a textbox?
anarki_jimbelSenior Developer
CERTIFIED EXPERT

Commented:
Try my code, it 's complete.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace charstonumber
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<char, int> char2numDictionary = new Dictionary<char, int>();
        private void button1_Click(object sender, EventArgs e)
        {
            // convert string to lower case and convert to array of characters
            char[] chars2convert = textBox1.Text.ToLower().ToCharArray();
            // our result - initialize it to 0
            int result = 0;
            // loop- through chars:
            foreach (char ch in chars2convert)
            {
                int charInt = char2numDictionary[ch];
                result = result + charInt;
            }
            this.lblResult.Text = "" + result;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            // initialize dictionary with characters and corresponding numbers
            char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
 
            for (int i = 0; i < chars.Length; i++)
            {
                // char is key, number is value
                char2numDictionary.Add(chars[i], i + 1); // indfexes strts from 0 - so add 1
            }
        }
    }
}

Open in new window

anarki_jimbelSenior Developer
CERTIFIED EXPERT

Commented:
Yeah, just forgot:

you have a TextBox1 at the form, the label calleslblResult, and a button called button 1.

If you need you may populate the dictionary with characters and related ascii numbers. I used alphabet index as a number (same as in your question).

Author

Commented:
Erron in line 7:

'WindowsFormsApplication7.Form1.Dispose(bool)': no suitable method found to override      

i am going to sleep now so i will see the other answers tomorrow! goodnight
Commented:
I created a new c# windows application and in Form1 added a textBox, a button and a label.  The label is named label2.  
Double clicking the button in design mode should take you to the button1 click event.
Here is what my button1 click event looks like.

		private void button1_Click(object sender, System.EventArgs e)
		{
			string inputField = this.textBox1.Text;
 
			System.Text.Encoding ascii = System.Text.Encoding.ASCII; 
			Byte[] encodedBytes = ascii.GetBytes(inputField); 
 
			// Initialize AsciiSum Field
			int AsciiSum = 0;
 
			foreach (Byte b in encodedBytes) 
			{
				if ((b>64) && (b<91))
				{
					// The Letter A is ASCII 65 and the Letter Z is ASCII 90
					// subtract 64 to get the value we need to add to our ascii sum
					AsciiSum += b-64;
				}
				else
				{
					if ((b>96) && (b<123))
					{
						// The Letter a is ASCII 97 and the Letter z is ASCII 122
						// subtract 96 to get the value we need to add to our ascii sum
						AsciiSum += b-96;
					}
				}
			}
			this.label2.Text = AsciiSum.ToString();
		}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
how can i make the button to check the radiobutton1 after it shows the AsciiSum to the label?

Commented:
kyrenator - I am not sure I understand you question.    I don't know why you would have a radiobutton on your form.
AsciiSum should be visible in a label on your main form.  The label is named label2.  
I will include a screenshot of my output.  

asciiconvert.bmp

Author

Commented:
ok thats fine but I need a radiobutton to continue my program! how i can tell the button to check a radiobutton after finding the sum?

Commented:
Can you provide a screenshot of your form with the radio button?  
I still do not understand the need for a radiobutton.  Were you provided with specifications on how the system needs to work.  If so, can you include them in your next message?
 
anarki_jimbelSenior Developer
CERTIFIED EXPERT

Commented:
kyrenator, my solution is tested and working. And provides the output you mrequested!
What error are you talking about?

To check a radiobutton add the following code to the Button1 handler - I just display result on a messagebox:

            if (radioButton1.Checked){
                MessageBox.Show("checked");
            }
            else{
                MessageBox.Show("not checked");
            }
        private void button1_Click(object sender, EventArgs e)
        {
            // convert string to lower case and convert to array of characters
            char[] chars2convert = textBox1.Text.ToLower().ToCharArray();
            // our result - initialize it to 0
            int result = 0;
            // loop- through chars:
            foreach (char ch in chars2convert)
            {
                int charInt = char2numDictionary[ch];
                result = result + charInt;
            }
            this.lblResult.Text = "" + result;
 
            if (radioButton1.Checked){
                MessageBox.Show("checked");
            }
            else{
                MessageBox.Show("not checked");
            }
        }

Open in new window

Author

Commented:
yes but how can make the button after doing all this things then to check to radio button its own!
anarki_jimbelSenior Developer
CERTIFIED EXPERT

Commented:
Your question is not quite clear -WHAT radiobuttons are you talking about? There wereno a word about radiobuttons in the original question. What type of check do you need? Explain the scenario pls.

Author

Commented:
ok leave i will find it!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.