Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone.

I have a problem in C# using Visual Studio Express 2013.

I am trying to create a code that replaces alphabet with numbers.

Let's say that user writes "ABCD" then it converts it into 1234.

A=1
B=2
...

It is in English alphabet so from 1 - 26 letters.

Letter A is 1 ... Z is 26.

Example:

COMPUTER = 3 15 13 16 21 20 5 18 or 31513162120518



C#
Console.Write("Name: ")
string name = Console.ReadLine();


Console.WritLine("Name in numbers: ")


I have done many thing but no success. If anyone has any idea I would appreciate it.

Thanks!
Posted
Updated 17-Nov-14 7:24am
v5
Comments
Zoltán Zörgő 17-Nov-14 13:00pm    
C# using VisualBasic ????
So you want a special 26 based number system?
Member 11241115 17-Nov-14 13:08pm    
Sorry. I messed up. I meant C# using Visual Studio Express 2013.
Zoltán Zörgő 17-Nov-14 13:10pm    
No problem.
Please update your question with a sample: how would you "convert" your name for example? I am ansking this because because there are more than one approaches.
Sergey Alexandrovich Kryukov 17-Nov-14 13:50pm    
This isn't too hard, would use just the basics of arithmetic and programming. I wonder why? :-)
And what have you tried so far?
—SA

There are many methods of doing this, but, ummm well instead of sending you out to them, let me clarify how can you convert an integer to its representative string value.

For example think of the following scenario,

C#
string alphabets = Console.ReadLine(); // returns string
string numeric = ""; // numeric representation
// string is an array of characters, so
foreach (char alphabet in alphabets) {
   // run a switch on each of the alphabet, 
   switch (alphabet) {
      case 'A':
        result += "1";
        break;
      case 'B':
        result += "2";
        break;
      case 'C':
        result += "3";
        break;
      /* 
       * execute this case for each of the character
       * in the alphabets and replace it and append it to the numeric variable
       * Just in case of special character, break the loop if you want to
       */
      default:
        throw new Exception("Special character is not allowed");
   }
}


This was just an example, there are many other methods of doing so.
 
Share this answer
 
v2
C#
// required
using System.Collections.Generic;
using System.Linq;
using System.Text;

private IEnumerable<int> lowerCaseRange = Enumerable.Range(97, 122);

private string stringToAlphaString(string source, bool addSpaces)
{
    StringBuilder sb = new StringBuilder();
    
    source = source.ToLower();
    
    foreach (char c in source)
    {
        if (lowerCaseRange.Contains(c))
        {
            sb.Append(c - 96);
            if (addSpaces) sb.Append(' ');
        }
    }
    
    return sb.ToString();
}

// test in some method or EventHandler
// string test = stringToAlphaString("and so it goes, Kurt Vonnegut", true);

// result: 1 14 4 19 15 9 20 7 15 5 19 11 21 18 20 22 15 14 14 5 7 21 20
Note that punctuation and numbers will be discarded in this implementation, although it would be easy to add handling them if you are clear about exactly how you want the output formatted.

In this case I made a specific decision to set up an EnumerableRange, rather than implement simpler tests for whether a character was a letter or a digit using:
if(char.IsLetter(c)) ... if(char.IsDigit(c))
The reason I made this choice is so that if I wanted to implement using an additional criterion based on some other range of characters, it would be easy to add.
 
Share this answer
 
v3
And here you have a LINQ solution based on your example:
C#
string.Join(" ","COMPUTER".Select(c => c - 64))
 
Share this answer
 
v5
Comments
BillWoodruff 17-Nov-14 16:04pm    
fyi: not necessary to use .ToCharArray()
Zoltán Zörgő 17-Nov-14 16:06pm    
Right! Good catch! Even neater!
Zoltán Zörgő 18-Nov-14 14:49pm    
Thanks to the unknown person for the upvote. I really like this method. LINQ rulez :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900