How can I list all latin (English alphabet) characters? - c#

I am looking for an efficient way of getting a list with all English (latin) characters.
A, B, C, .... , Z
I really don't want a constructor like this:
// nasty way to create list of English alphabet
List<string> list = new List<string>();
list.Add("A");
list.Add("B");
....
list.Add("Z");
// use the list
...
If you are curius on how is this usable, I am creating a bin-tree mechanism.

You can do this with a for loop:
List<char> list = new List<char>();
for (char c = 'A'; c <= 'Z'; ++c) {
list.Add(c);
}
If you want a List<string> instead of a List<char> use list.Add(c.ToString()); instead.
Note that this works only because the letters A - Z occur in a consecutive sequence in Unicode (code points 65 to 90). The same approach does not necessarily work for other alphabets.

Here:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
This string is a list of characters.
Use either ToCharArray or the LINQ ToList to convert to an enumerable of your choice, though you can already access each item through the Chars indexer of the string.

Using LINQ
int charactersCount = 'Z' - 'A' + 1;
IList<char> all = Enumerable.Range('A', charactersCount)
.Union(Enumerable.Range('a', charactersCount))
.Select(i => (char)i)
.ToList();

There's no built in way to get a list of strings that correspond to each character. You can get an IEnumerable with the following code, which will probably suit your purposes. You could also just stick with the array in the from section.
var letters = from letter in "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
select letter.ToString();

Here is my solution (eventually)
const string driveLetters = "DEFGHIJKLMNOPQRSTUVWXYZ";
List<string> allDrives = new List<string>(driveLetters.Length);
allDrives = (from letter
in driveLetters.ToCharArray()
select letter).ToList();
I ended up with this solution because initially my goal was to create a list of all available drives in windows. This is the actual code:
const string driveLetters = "DEFGHIJKLMNOPQRSTUVWXYZ";
const string driveNameTrails = #":\";
List<string> allDrives = (from letter
in driveLetters.ToCharArray()
select letter + driveNameTrails).ToList();
return allDrives;

The simplest way -
int start = (int) 'A';
int end = (int) 'Z';
List<char> letters = new List<char>();
for (int i = start; i <= end; i++)
{
letters.Add((char)i);
}
Same way but less code -
IEnumerable<char> linqLetters = Enumerable.Range(start, end - start + 1).Select(t => (char)t);

Generate string and convert to array
string str = "abcdefghijklmnopqrstuvwxyz"; //create characters
char[] arr; //create array
arr = str.ToCharArray(0, 26); //You can change the number to adjust this list
Get the value in the array
char getchar = arr[17];//get "r"

Related

Detecting and modifying ListBox entries that contain digits

My program has about 25 entries, most of them string only. However, some of them are supposed to have digits in them, and I don't need those digits in the output (output should be string only). So, how can I "filter out" integers from strings?
Also, if I have integers, strings AND chars, how could I do it (for example, one ListBox entry is E#2, and should be renamed to E# and then printed as output)?
Assuming that your entries are in a List<string>, you can loop through the list and then through each character of each entry, then check if it is a number and remove it. Something like this:
List<string> list = new List<string>{ "abc123", "xxx111", "yyy222" };
for (int i = 0; i < list.Count; i++) {
var no_numbers = "";
foreach (char c in list[i]) {
if (!Char.IsDigit(c))
no_numbers += c;
}
list[i] = no_numbers;
}
This only removes digits as it seems you wanted from your question. If you want to remove all other characters except letters, you can change the logic a bit and use Char.IsLetter() instead of Char.IsDigit().
You can remove all numbers from a strings with this LINQ solution:
string numbers = "Ho5w ar7e y9ou3?";
string noNumbers = new string(numbers.Where(c => !char.IsDigit(c)).ToArray());
noNumbers = "How are you?"
But you can also remove all numbers from a string by using a foreach loop :
string numbers = "Ho5w ar7e y9ou3?";
List<char> noNumList = new List<char>();
foreach (var c in numbers)
{
if (!char.IsDigit(c))
noNumList.Add(c);
}
string noNumbers = string.Join("", noNumList);
If you want to remove all numbers from strings inside a collection :
List<string> myList = new List<string>() {
"Ho5w ar7e y9ou3?",
"W9he7re a3re y4ou go6ing?",
"He2ll4o!"
};
List<char> noNumList = new List<char>();
for (int i = 0; i < myList.Count; i++)
{
foreach (var c in myList[i])
{
if(!char.IsDigit(c))
noNumList.Add(c);
}
myList[i] = string.Join("", noNumList);
noNumList.Clear();
}
myList Output :
"How are you?"
"Where are you going?"
"Hello!"
I don't know exactly what is your scenario, but given a string, you can loop through its characters, and if it's a number, discard it from output.
Maybe this is what you're looking for:
string entry = "E#2";
char[] output = new char[entry.Length];
for(int i = 0, j =0; i < entry.Length ; i++)
{
if(!Char.IsDigit(entry[i]))
{
output[j] = entry[i];
j++;
}
}
Console.WriteLine(output);
I've tried to give you a simple solution with one loop and two index variables, avoiding string concatenations that can make performance lacks.
See this example working at C# Online Compiler
If i am not wrong,maybe this is how your list looks ?
ABCD123
EFGH456
And your expected output is :
ABCD
EFGH
Is that correct?If so,assuming that it's a List<string>,then you can use the below code :
list<string> mylist = new list<string>;
foreach(string item in mylist)
{
///To get letters/alphabets
var letters = new String(item.Where(Char.IsLetter).ToArray());
///to get special characters
var letters = new String(item.Where(Char.IsSymbol).ToArray())
}
Now you can easily combine the codes :)

c# switch all letters in a string for a different pre-chosen letter

Whilst working on a simple project I was trying to come up with a very basic cryptographic system which will allow me to switch letters in a message to another pre-chosen letter. So far I have tried a few ways but have so far been unsuccessful in my attempts. (*NOTE: This is not for anything other then learning the basics of c# so randomness and security is not important in this case, I simply want to turn one letter into another for the sake of learning how to do it)
so first I started by defining some strings such as this
string a = "a";
string b = "b";
string c = "d";
..... //continues to string z = "z"
next I tried to create a new string based on the values that have been input in to a textbox called PlainTextBox and place them inside a separate textbox called ChangedTextBox. this code is triggered with a button click event.
string str = PlainTextBox.Text;
char[] array = str.ToCharArray();
array[int.Parse(a)] = 'x';
array[int.Parse(b)] = 'y';
array[int.Parse(c)] = 'z';
.......// continues to (z)
str = new string(array);
ChangedTextBox.Text = str;
but this code throws an exception because the input is not a valid integer. the basic Idea is that if the user types "abc" in the PlainTextBox and pushes a button, the ChangedTextBox should show "xyz" but should be inclusive of the whole text in PlainTextBox, switching every letter in the message to its chosen counterpart.
Besides the error I receive, this code seems very cumbersome and inefficient.
Is there a faster way to achieve this result?
Just for completeness I will also include information, that what you are doing is called Caesar cipher
You could define yourself a proper Dictionary
var mapping = new Dictionary<char, char>()
{
{ 'a', 'x' },
{ 'b', 'y' },
{ 'c', 'z' }
// other letters
}
in which you would assign every original letter the letter it should be converted to. Then you could use this dictionary
ChangedTextBox.Text = new string(PlainTextBox.Text.Select(letter => mapping[letter].ToArray());
You've chosen wrong collection type (array) for mapping; dictionary is much more convenient
private static Dictionary<char, char> m_Mapping = new Dictionary<char, char>() {
{'a', 'x'}, // a -> x
{'b', 'y'}, // b -> y
...
};
Then implement the encoding
// I'd rather used Linq
private static String Encode(String value) {
// Simplest: we don't check if the actual character can be mapped
return String.Concat(value.Select(c => m_Mapping[c]));
}
But your (amended) implementation is good enough:
private static String Encode(string str) {
char[] array = str.ToCharArray();
for (int i = 0; i < array.Length; ++i) {
// Simplest: we don't check if the actual character can be mapped
array[i] = m_Mapping[array[i]];
}
return new string(array);
}
Finally, add up UI:
ChangedTextBox.Text = Encode(PlainTextBox.Text);
Edit: in general case, when m_Mapping doesn't contain records for some characters (e.g. for new line \n) and so we want to preserve these characters intact we can't use direct m_Mapping[...] but should implement, say, EncodeChar.
private static char EncodeChar(char value) {
char result;
if (m_Mapping.TryGetValue(value, out result))
return result;
else
return value;
}
And put EncodeChar(...) instead of m_Mapping[...]
private static String Encode(String value) {
return String.Concat(value.Select(EncodeChar(c)));
}
Your version
private static String Encode(string str) {
char[] array = str.ToCharArray();
for (int i = 0; i < array.Length; ++i) {
array[i] = EncodeChar(array[i]);
return new string(array);
}
Probably the best solution is using a Dictionary, as other answers had said. But if I understand what you want, you want to just change one letter by that letter plus an offset in a kind of "circular" way. This solution would do something like that (with "abcd" as input it would return "xyza"):
string input = "abcd";
char startChar = 'x';
char lastChar = 'z';
char firstChar = 'a';
byte[] asciiBytes=Encoding.ASCII.GetBytes(input);
byte[] encriptedByteArray = new byte[asciiBytes.Length];
int val = (int)startChar-(int)firstChar;
int i = 0;
foreach(byte b in asciiBytes)
{
var a=b + val;
if (a>(int)lastChar)
{
a = firstChar+(a-lastChar)-1;
}
encriptedByteArray[i] = (byte)a;
i++;
}
string encriptedArray = System.Text.Encoding.UTF8.GetString(encriptedByteArray);
With this solution you can change the offsety easily (changing startChar). It has room for improvement though, for example it only works on lower letters from a-z, it could be changed to be more extensive.
int.Parse(a)
will of course throw InvalidCastException because a is a string as you've defined it
string a = "a";

Breaking Down & Rearranging String into all possible combinations

I want to break down and rearranging a string into all possible combinations
Say I have a String: ABCDEF
I want to break it down and output all possible combinations
Combination(6,6) = 1
ABCDEF
Combination(6,5) = 6
BCDEF
ACDEF
ABDEF
ABCEF
ABCDF
ABCDE
Combination(6,4) = 15
BCDE
ACDE
ABDE
ABCE
....
....
....
etc.
Combination(6,3) = 20
BCD
ACD
...
etc.
Combination(6,2) = 15
BC
AB
etc.
However the ouptut must also be arranged into alphabetical order.
How will I do this?
Thanks! Any help will be appreciated!
You can get the algorithm (actually a few of them) from Knuth Volume 4, Fascicle 3 but you'll have to convert it from his math notation to C#.
Update: As I think about this more, Fascicle 2 (Generating Permutations) is actually more helpful. You can download it free from http://www-cs-faculty.stanford.edu/~knuth/fasc2b.ps.gz though you'll need gunzip and a PostScript previewer to read it. Generating the subsets of string "ABCDE" is the easy part. Convert it to an array {'A', 'B', 'C', 'D', 'E'}, run a for loop from 0 to 2^N-1 where N is the array length, and treat each value as a bitmask of the elements you're keeping. Thus 00001, 00010, 00011,... gives you "A", "B", "AB",...
The hard part is generating all the permutations of each subset, so you get "ABC", "BAC", "CAB", etc. A brute force algorithm (like in one of the other answers) will work but will get very slow if the string is long. Knuth has some fast algorithms, some of which will generate the permutations in alphabetical order if the original string was sorted in the first place.
Well, to expand on my comment, how I got past this problem was transforming the string into a hash that doesn't care the order of the letters. The hash works by taking each unique letter, then a :, then the number of times that letter occurs.
So test = e:1,s:1,t:2
Then if somebody looks for the world tset, it would generate the same hash (e:1,s:1,t:2), and bam you have a match.
I just ran a word list (of about 20 million words), generated a hash for each one of them, and put it in a mysql table, I can find all permutations of a word (that are still words themselves, aka ered will return deer and reed) in seconds.
You can generate each permutation by incrementing a counter and converting the counter value to base n where n is the number of letters in your input. Discard any values containing repeating letters and what you have left are the possible scrabble words in alphabetic order assuming your array was sorted.
You will have to count up to (n^(n-1))*(n+1) to get the e*n! possible scrabble words.
char[] Letters = new char[] { 'A', 'B', 'C', 'D', 'E', 'F' };
// calculate e*n! (int)Math.Floor(Math.E * Math.Factorial(Letters.Length))
int x = 0;
for (int i = 1; i <= Letters.Length; i++)
x = (x + 1) * i;
for (int i = 1; x > 0; i++)
{
string Word = BaseX(i, Letters.Length, Letters);
if (NoRepeat(Word))
{
Console.WriteLine(Word);
x--;
}
}
BaseX returns the string representation of Value for the given Base and specified Symbols:
string BaseX(int Value, int Base, char[] Symbols)
{
StringBuilder s = new StringBuilder();
while (Value > Base)
{
s.Insert(0, Symbols[Value % Base]);
Value /= Base;
}
s.Insert(0, Symbols[Value - 1]);
return s.ToString();
}
NoRepeat returns false if any letter occurs more than once:
bool NoRepeat(string s)
{
bool[] Test = new bool[256];
foreach (char c in s)
if (Test[(byte)c])
return false;
else
Test[(byte)c] = true;
return true;
}
Sort the string in alphabet order. Say ABCDEF (your example)
Prepare a map between index and character
map[0] = 'A'; map[1] = 'B'; ... map[5] = 'F'
3 . Now your job is a lot more simple: find all combinations of number in which the later number is larger than the former
Combination(6,3):
for (int i = 0; i < 6 - 2; i++)
for (int j = i + 1; j < 6 - 1; j++)
for (int k = j + 1; k < 6; k++)
{
string strComb = map[i] + map[j] + map[k];
}
This is mainly the idea, you could improve in your own way.
Contact me if you want more detail!
You can use this:
static List<string> list = new List<string>();
static string letters = "bcdehijkmnopqrstuvwxyz";
static void Combine(string combinatory)
{
if(combinatory.Length < letters.Length)
{
Parallel.ForEach(letters, l =>
{
if (!combinatory.Contains(l)) Combine(combinatory + l);
});
} else
{
list.Add(combinatory);
Console.WriteLine(combinatory);
}
}
It will add to the list List all the possible combinations.
Then you can use the Sort() method in order to sort the list.

Convert a word into character array

How do I convert a word into a character array?
Lets say i have the word "Pneumonoultramicroscopicsilicovolcanoconiosis" yes this is a word ! I would like to take this word and assign a numerical value to it.
a = 1
b = 2
... z = 26
int alpha = 1;
int Bravo = 2;
basic code
if (testvalue == "a")
{
Debug.WriteLine("TRUE A was found in the string"); // true
FinalNumber = Alpha + FinalNumber;
Debug.WriteLine(FinalNumber);
}
if (testvalue == "b")
{
Debug.WriteLine("TRUE B was found in the string"); // true
FinalNumber = Bravo + FinalNumber;
Debug.WriteLine(FinalNumber);
}
My question is how do i get the the word "Pneumonoultramicroscopicsilicovolcanoconiosis" into a char string so that I can loop the letters one by one ?
thanks in advance
what about
char[] myArray = myString.ToCharArray();
But you don't actually need to do this if you want to iterate the string. You can simply do
for( int i = 0; i < myString.Length; i++ ){
if( myString[i] ... ){
//do what you want here
}
}
This works since the string class implements it's own indexer.
string word = "Pneumonoultramicroscopicsilicovolcanoconiosis";
char[] characters = word.ToCharArray();
Voilá!
you can use simple for loop.
string word = "Pneumonoultramicroscopicsilicovolcanoconiosis";
int wordCount = word.Length;
for(int wordIndex=0;wordIndex<wordCount; wordIndex++)
{
char c = word[wordIndex];
// your code
}
You can use the Linq Aggregate function to do this:
"wordsto".ToLower().Aggregate(0, (running, c) => running + c - 97);
(This particular example assumes you want to treat upper- and lower-case identically.)
The subtraction of 97 translates the ASCII value of the letters such that 'a' is zero. (Obviously subtract 96 if you want 'a' to be 1.)
you can use ToCharArray() method of string class
string strWord = "Pneumonoultramicroscopicsilicovolcanoconiosis";
char[] characters = strWord.ToCharArray();

Converting a string number into sequence of digits - .Net 2.0

Given a string
string result = "01234"
I want to get the separate integers 0,1,2,3,4 from the string.
How to do that?
1
The following code is giving me the ascii values
List<int> ints = new List<int>();
foreach (char c in result.ToCharArray())
{
ints.Add(Convert.ToInt32(c));
}
EDIT: I hadn't spotted the ".NET 2.0" requirement. If you're going to do a lot of this sort of thing, it would probably be worth using LINQBridge, and see the later bit - particularly if you can use C# 3.0 while still targeting 2.0. Otherwise:
List<int> integers = new List<int>(text.Length);
foreach (char c in text)
{
integers.Add(c - '0');
}
Not as neat, but it will work. Alternatively:
List<char> chars = new List<char>(text);
List<int> integers = chars.ConvertAll(delegate(char c) { return c - '0'; });
Or if you'd be happy with an array:
char[] chars = text.ToCharArray();
int[] integers = Arrays.ConvertAll<char, int>(chars,
delegate(char c) { return c - '0'; });
Original answer
Some others have suggested using ToCharArray. You don't need to do that - string already implements IEnumerable<char>, so you can already treat it as a sequence of characters. You then just need to turn each character digit into the integer representation; the easiest way of doing that is to subtract the Unicode value for character '0':
IEnumerable<int> digits = text.Select(x => x - '0');
If you want this in a List<int> instead, just do:
List<int> digits = text.Select(x => x - '0').ToList();
Loop the characters and convert each to a number. You can put them in an array:
int[] digits = result.Select(c => c - '0').ToArray();
Or you can loop through them directly:
foreach (int digit in result.Select(c => c - '0')) {
...
}
Edit:
As you clarified that you are using framework 2.0, you can apply the same calculation in your loop:
List<int> ints = new List<int>(result.Length);
foreach (char c in result) {
ints.Add(c - '0');
}
Note: Specify the capacity when you create the list, that elliminates the need for the list to resize itself. You don't need to use ToCharArray to loop the characters in the string.
You could use LINQ:
var ints = result.Select(c => Int32.Parse(c.ToString()));
Edit:
Not using LINQ, your loop seems good enough. Just use Int32.Parse instead of Convert.ToInt32:
List<int> ints = new List<int>();
foreach (char c in result.ToCharArray())
{
ints.Add(Int32.Parse(c.ToString()));
}
string result = "01234";
List<int> list = new List<int>();
foreach (var item in result)
{
list.Add(item - '0');
}
Index into the string to extract each character. Convert each character into a number. Code left as an exercise for the reader.
another solution...
string numbers = "012345";
List<int> list = new List<int>();
foreach (char c in numbers)
{
list.Add(int.Parse(c.ToString()));
}
no real need to do a char array from the string since a string can be enumerated over just like an array.
also, the ToString() makes it a string so the int.Parse will give you the number instead of the ASCII value you get when converting a char.
List<int> ints = new List<int>();
foreach (char c in result.ToCharArray())
{
ints.Add(Convert.ToInt32(c));
}
static int[] ParseInts(string s) {
int[] ret = new int[s.Length];
for (int i = 0; i < s.Length; i++) {
if (!int.TryParse(s[i].ToString(), out ret[i]))
throw new InvalidCastException(String.Format("Cannot parse '{0}' as int (char {1} of {2}).", s[i], i, s.Length));
}
return ret;
}

Categories

Resources