[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 595
  • Last Modified:

Letters to numbers C#

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????
0
kyrenator
Asked:
kyrenator
  • 7
  • 5
  • 4
1 Solution
 
anarki_jimbelCommented:
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?
0
 
kyrenatorAuthor Commented:
yes if it is easy for you!
0
 
Jammer59Commented:
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kyrenatorAuthor Commented:
and how can i connect this with a textbox?
0
 
anarki_jimbelCommented:
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

0
 
anarki_jimbelCommented:
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).
0
 
kyrenatorAuthor 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
0
 
Jammer59Commented:
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

0
 
kyrenatorAuthor Commented:
how can i make the button to check the radiobutton1 after it shows the AsciiSum to the label?
0
 
Jammer59Commented:
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
0
 
kyrenatorAuthor 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?
0
 
Jammer59Commented:
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?
 
0
 
anarki_jimbelCommented:
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

0
 
kyrenatorAuthor Commented:
yes but how can make the button after doing all this things then to check to radio button its own!
0
 
anarki_jimbelCommented:
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.
0
 
kyrenatorAuthor Commented:
ok leave i will find it!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now