Convert character to its alphabet integer position?
Programming 101:
char c = 'A';
//char c = 'b'; you may use lower case character.
int index = char.ToUpper(c) - 64;//index == 1
Convert characters of the string in alphabet integer position?
You return inside the loop - which is not your desired behaviour. With some minor refactoring, we can clean this up a bit:
private int letterNumber(string letter)
{
int sum = 0;
for (int i = 0; i < letter.Length; i++)
{
char c = letter[i]; // declare it in loop - you overwrite it here anyway
sum += char.ToUpper(c) - 64;
}
return sum;
}
Converting letter position in the alphabet to numbers in C#
Try:
var x = 'A' - 'A' + 1 //01
var x = 'Z' - 'A' + 1 //26
How to convert letters to numbers with Javascript?
You can get a codepoint* from any index in a string using String.prototype.charCodeAt
. If your string is a single character, you’ll want index 0
, and the code for a
is 97 (easily obtained from JavaScript as 'a'.charCodeAt(0)
), so you can just do:
s.charCodeAt(0) - 97
And in case you wanted to go the other way around, String.fromCharCode
takes Unicode codepoints* and returns a string.
String.fromCharCode(97 + n)
* not quite
Convert a Letter to its alphabet numerical Rank
You start by simply getting its Unicode value:
int charValue = Convert.ToInt32('A');
Then account for where 'A' is on the Unicode table (65)
int rank = charValue - 65;
Note that this won't work for lower case letters, as they are in a different position. You could use ToLower
or ToUpper
on the string version of the character to nullify this (as in the other answer).
Convert numeric List 1,2,3,4 to Alphabetic List A,B,C,D
You could do a simple math. ie:
void Main()
{
var intList = Enumerable.Range(1, 1000);
var alphaList = intList.Select(x => ToAlpha(x));
foreach (var x in alphaList)
{
Console.WriteLine(x);
}
}
private static string ToAlpha(int i)
{
string result = "";
do
{
result = (char)((i-1) % 26 + 'A') + result;
i = (i-1)/26;
} while (i > 0);
return result;
}
Replace a letter with its alphabet position
The Kata works with this code. Try with this one:
function alphabetPosition(text) { var result = ""; for (var i = 0; i < text.length; i++) { var code = text.toUpperCase().charCodeAt(i) if (code > 64 && code < 91) result += (code - 64) + " "; }
return result.slice(0, result.length - 1);}console.log(alphabetPosition("The sunset sets at twelve o' clock."));
Converting letters to alphabet position with two JLists
So given...
(ex. A to 01) And if it's not an English alphabet letter then leaving it as it is. Capitalization doesn't matter (a and A is still 01) and spaces should be kept.
This raises some interesting points:
- We don't care about non-english characters, so we can dispense with issues around UTF encoding
- Capitalization doesn't matter
- Spaces should be kept
The reason these points are interesting to me is it means we're only interested in a small subset of characters (1-26). This immediately screams "ASCII" to me!
This provides an immediate lookup table which doesn't require us to produce anything up front, it's immediately accessible.
A quick look at any ascii table provides us with all the information we need. A-Z
is in the range of 65-90
(since we don't care about case, we don't need to worry about the lower case range.
But how does that help us!?
Well, this now means the primary question becomes, "How do we convert a char
to an int
?", which is amazingly simple! A char
can be both a "character" and a "number" at the same time, because of the ASCII encoding support!
So if you were to print out (int)'A'
, it would print 65
! And since all the characters are in order, we just need to subtract 64
from 65
to get 1
!
That's basically your entire problem solved right there!
Oh, okay, you need to deal with the edge cases of characters not falling between A-Z
, but that's just a simple if statement
A solution based on the above "might" look something like...
public static String convert(String text) {
int offset = 64;
StringBuilder sb = new StringBuilder(32);
for (char c : text.toCharArray()) {
char input = Character.toUpperCase(c);
int value = ((int) input) - offset;
if (value < 1 || value > 25) {
sb.append(c);
} else {
sb.append(String.format("%02d", value));
}
}
return sb.toString();
}
Now, there are a number of ways you might approach this, I've chosen a path based on my understanding of the problem and my experience.
And based on your example input...
String[] test = {"Apple!", "stack Overflow", "über"};
for (String value : test) {
System.out.println(value + " = " + convert(value));
}
would produce the following output...
Apple! = 0116161205!
stack Overflow = 1920010311 1522051806121523
über = ü020518
Converting alphabet letter to alphabet position in PHP
By using the ascii value:
ord(strtoupper($letterOfAlphabet)) - ord('A') + 1
in ASCII the letters are sorted by alphabetic order, so...
How to get numeric position of alphabets in java?
String str = "abcdef";
char[] ch = str.toCharArray();
for(char c : ch){
int temp = (int)c;
int temp_integer = 96; //for lower case
if(temp<=122 & temp>=97)
System.out.print(temp-temp_integer);
}
Output:
123456
@Shiki for Capital/UpperCase letters use the following code:
String str = "DEFGHI";
char[] ch = str.toCharArray();
for(char c : ch){
int temp = (int)c;
int temp_integer = 64; //for upper case
if(temp<=90 & temp>=65)
System.out.print(temp-temp_integer);
}
Output:
456789
Related Topics
How to Write Data to a Text File Without Overwriting the Current Data
Passing a List of Objects into an MVC Controller Method Using Jquery Ajax
How to Count Rows Within Entityframework Without Loading Contents
How Does Httpcontext.Current.User.Identity.Name Know Which Usernames Exist
How to Correctly Write Parallel.For With Async Methods
Redirect from Controller to Another View of Another Controller
How to Have a a Razor Action Link Open in a New Tab
Select Multiple Fields from List in Linq
How to Determine If a Json Object Contains Only a Specific Key
Asp Core Webapi Test File Upload Using Postman
Browser Back Button Navigates to Login Page When User Is Still Logged In
Check If Task Is Already Running Before Starting New
Deserialize Multiple Xml Elements With the Same Name Through Xmlserializer Class in C#
Create Global Variable in .Net Core 2.0 to Use in Razor Views