The case, breaks, and loops with c# - c#

My application works if I type in 1 character, such as A. It will give me 10 2's,
but I want it to work on all 10 digits I type in. What am I doing wrong?
I want it so I can type in 1800HELLO2 and it will give me all digits.
class Program
{
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
while (x < 10)
{
switch (userInput)
{
case "1":
userInput = "1";
x++;
break;
case "A":
case "B":
case "C":
case "2":
userInput = "2";
x++;
break;
case "D":
case "E":
case "F":
case "3":
userInput = "3";
x++;
break;
case "G":
case "H":
case "I":
case "4":
userInput = "4";
x++;
break;
case "J":
case "K":
case "L":
case "5":
userInput = "5";
x++;
break;
case "M":
case "N":
case "O":
case "6":
userInput = "6";
x++;
break;
case "P":
case "Q":
case "R":
case "7":
userInput = "7";
x++;
break;
case "S":
case "T":
case "U":
case "8":
userInput = "8";
x++;
break;
case "V":
case "W":
case "X":
case "Y":
case "Z":
userInput = "9";
x++;
break;
case "0":
userInput = "0";
break;
}
Console.WriteLine(userInput);
}
}
}
}

Your user input can be any number of characters (the ReadLine() function will read N characters until you press ENTER) but your switch statement doesn't check individual characters of your input - it only checks a number of 1-character strings.
This means that even if you type 1-800-333-1111, you'll never check it because it's not a 1-character string like the various cases you have in the switch.
You need to iterate through each character one-by-one in the input string and check the individual characters. For example:
if ( userinput != null )
{
userinput = userinput.ToUpper ();
for ( int i = 0; i < userinput.Length; i++ )
{
switch ( userinput[i] )
{
case '1':
case 'A':
...
break;
...
default:
// Handle invalid characters here
break;
}
}
}
Notice that the various case values are single characters (using the '), not 1-character strings.
Do note, that it's not a very good idea to hardcode the length of the phone number as a number in the code. Different users may enter the phone numbers differently - some may use spaces or dashes as separators, some may only enter the digit. In these cases the length of the input string will be different. Some users may even accidentally enter multiple spaces or put in ( and ) for the area code.
You should validate the input before you even check or you shouldn't rely on the number of input digits as you iterate through the input.

Your approach isn't really correct. You're switching on userInput every time, when really you want to check each character in the string. Study the code below:
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Please enter the 10 digit telephone number. ");
string userInput = Console.ReadLine();
// Maybe do some validation here, check the length etc
Char output;
foreach (Char c in userInput) {
switch (c) {
case 'A':
case 'B':
case 'C':
output = '2';
break;
case 'D':
case 'E':
case 'F':
output = '3';
break;
case 'G':
case 'H':
case 'I':
output = '4';
break;
case 'J':
case 'K':
case 'L':
output = '5';
break;
case 'M':
case 'N':
case 'O':
output = '6';
break;
case 'P':
case 'Q':
case 'R':
output = '7';
break;
case 'S':
case 'T':
case 'U':
output = '8';
break;
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
output = '9';
break;
default:
output = c;
break;
}
Console.Write(output);
}
Console.WriteLine();
Console.ReadLine();
}
}
We use a foreach loop so we aren't hard coding the length, which gives flexibility.

Try this instead:
class Program
{
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
// Did the user type in more than 10 characters?
if(userInput.Length > 10)
{
// Get the first ten letters, no matter how many letters the user entered
userInput = userInput.Substring(0, 10);
}
// Force values to upper case for comparison
userInput = userInput.ToUpper();
string systemOutput = String.Empty;
foreach(var c in userInput)
{
switch (c)
{
case "1":
systemOutput += "1";
break;
case "A":
case "B":
case "C":
case "2":
systemOutput += "2";
break;
case "D":
case "E":
case "F":
case "3":
systemOutput += "3";
break;
case "G":
case "H":
case "I":
case "4":
systemOutput += "4";
break;
case "J":
case "K":
case "L":
case "5":
systemOutput += "5";
break;
case "M":
case "N":
case "O":
case "6":
systemOutput += "6";
break;
case "P":
case "Q":
case "R":
case "7":
systemOutput += "7";
break;
case "S":
case "T":
case "U":
case "8":
systemOutput += "8";
break;
case "V":
case "W":
case "X":
case "Y":
case "Z":
systemOutput += "9";
break;
case "0":
systemOutput += "0";
break;
}
}
Console.WriteLine(systemOutput);
}
}

You can take benefit of Dictionary for this purpose:
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
Dictionary<string,string> dict = new Dictionary<string,string>();
dict.Add("1","1");
dict.Add("ABC2","2");
dict.Add("DEF3","3");
dict.Add("GHI4","4");
dict.Add("JKL5","5");
dict.Add("MNO6","6");
dict.Add("PQR7","7");
dict.Add("STU8","8");
dict.Add("VWXYZ9","9");
dict.Add("0","0");
userInput = string.Join("",userInput.Select(c=>dict.First(k=>k.Key.Contains(c)).Value).ToArray());
Console.WriteLine(userInput);
}
or even more concise:
static void Main(string[] args)
{
int x = 0;
string userInput;
Console.WriteLine("Please enter the 10 digit telephone number. ");
userInput = Console.ReadLine();
string[] s = "0,1,ABC2,DEF3,GHI4,JKL5,MNOP6,PQR7,STU8,VWXYZ9".Split(',');
userInput = string.Join("",userInput.Select(c=>s.Select((x,i)=>new{x,i})
.First(k=>k.x.Contains(c)).i).ToArray());
Console.WriteLine(userInput);
}

Related

Passing a variable from a method to a button click event

I want to be able to pass upperEncodedMsg into the text property of msgLabel at the bottom of my code.
namespace ProgrammingAssignmentDecoder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Encode()
{
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToCharArray();
for (int i = 0; i < encodeArray.Length; i++)
{
char letter = (encodeArray[i]);
switch (letter)
{
case 'a':
case 'A':
encodeArray[i] = 't';
break;
case 'b':
case 'B':
encodeArray[i] = 'u';
break;
case 'c':
case 'C':
encodeArray[i] = 'v';
break;
case 'd':
case 'D':
encodeArray[i] = 'w';
break;
case 'e':
case 'E':
encodeArray[i] = 'x';
break;
case 'f':
case 'F':
encodeArray[i] = 'y';
break;
case 'g':
case 'G':
encodeArray[i] = 'z';
break;
case 'h':
case 'H':
encodeArray[i] = 'a';
break;
case 'i':
case 'I':
encodeArray[i] = 'b';
break;
case 'j':
case 'J':
encodeArray[i] = 'c';
break;
case 'k':
case 'K':
encodeArray[i] = 'd';
break;
case 'l':
case 'L':
encodeArray[i] = 'e';
break;
case 'm':
case 'M':
encodeArray[i] = 'f';
break;
case 'n':
case 'N':
encodeArray[i] = 'g';
break;
case 'o':
case 'O':
encodeArray[i] = 'h';
break;
case 'p':
case 'P':
encodeArray[i] = 'i';
break;
case 'q':
case 'Q':
encodeArray[i] = 'j';
break;
case 'r':
case 'R':
encodeArray[i] = 'k';
break;
case 's':
case 'S':
encodeArray[i] = 'l';
break;
case 't':
case 'T':
encodeArray[i] = 'm';
break;
case 'u':
case 'U':
encodeArray[i] = 'n';
break;
case 'v':
case 'V':
encodeArray[i] = 'o';
break;
case 'w':
case 'W':
encodeArray[i] = 'p';
break;
case 'x':
case 'X':
encodeArray[i] = 'q';
break;
case 'y':
case 'Y':
encodeArray[i] = 'r';
break;
case 'z':
case 'Z':
encodeArray[i] = 's';
break;
}
}
}
static string upperEncoded(char[] encodeArray, string upperEncodedMsg)
{
string encodedMsg = new string(encodeArray);
upperEncodedMsg = encodedMsg.ToUpper();
return upperEncodedMsg;
}
private void clearBtn_Click(object sender, EventArgs e)
{
messageTxt.Text = string.Empty;
msgLabel.Text = string.Empty;
processedMessageLabel.Text = "Processed Message: ";
}
private void encodeBtn_Click(object sender, EventArgs e)
{
Encode();
if (messageTxt.TextLength == 0)
{
MessageBox.Show("There is no message to Encode");
}
else
{
processedMessageLabel.Text = "Encoded Message: ";
msgLabel.Visible = true;
}
msgLabel.Text = upperEncodedMsg;
}
}
}
I tried to keep your structure in tact, there are a few things that could be done in less characters and in less complicated ways, but as you are learning the language practice is the only thing that can improve your skills :) So well done.
namespace ProgrammingAssignmentDecoder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string upperEncodedMsg = null;
public void Encode()
{
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToCharArray();
for (int i = 0; i < encodeArray.Length; i++)
{
char letter = (encodeArray[i]);
switch (letter)
{
case 'a':
case 'A':
encodeArray[i] = 't';
break;
case 'b':
case 'B':
encodeArray[i] = 'u';
break;
case 'c':
case 'C':
encodeArray[i] = 'v';
break;
case 'd':
case 'D':
encodeArray[i] = 'w';
break;
case 'e':
case 'E':
encodeArray[i] = 'x';
break;
case 'f':
case 'F':
encodeArray[i] = 'y';
break;
case 'g':
case 'G':
encodeArray[i] = 'z';
break;
case 'h':
case 'H':
encodeArray[i] = 'a';
break;
case 'i':
case 'I':
encodeArray[i] = 'b';
break;
case 'j':
case 'J':
encodeArray[i] = 'c';
break;
case 'k':
case 'K':
encodeArray[i] = 'd';
break;
case 'l':
case 'L':
encodeArray[i] = 'e';
break;
case 'm':
case 'M':
encodeArray[i] = 'f';
break;
case 'n':
case 'N':
encodeArray[i] = 'g';
break;
case 'o':
case 'O':
encodeArray[i] = 'h';
break;
case 'p':
case 'P':
encodeArray[i] = 'i';
break;
case 'q':
case 'Q':
encodeArray[i] = 'j';
break;
case 'r':
case 'R':
encodeArray[i] = 'k';
break;
case 's':
case 'S':
encodeArray[i] = 'l';
break;
case 't':
case 'T':
encodeArray[i] = 'm';
break;
case 'u':
case 'U':
encodeArray[i] = 'n';
break;
case 'v':
case 'V':
encodeArray[i] = 'o';
break;
case 'w':
case 'W':
encodeArray[i] = 'p';
break;
case 'x':
case 'X':
encodeArray[i] = 'q';
break;
case 'y':
case 'Y':
encodeArray[i] = 'r';
break;
case 'z':
case 'Z':
encodeArray[i] = 's';
break;
}
}
foreach (char eachChar in encodeArray) {
upperEncodedMsg += eachChar;
}
}
public void upperEncoded()
{
if (upperEncodedMsg != null)
{ upperEncodedMsg = upperEncodedMsg.ToUpper(); }
}
private void clearBtn_Click(object sender, EventArgs e)
{
messageTxt.Text = string.Empty;
msgLabel.Text = string.Empty;
processedMessageLabel.Text = "Processed Message: ";
}
private void encodeBtn_Click(object sender, EventArgs e)
{
if (messageTxt.TextLength == 0)
{
MessageBox.Show("There is no message to Encode");
}
else
{
Encode();
upperEncoded();
processedMessageLabel.Text = "Encoded Message: ";
msgLabel.Visible = true;
}
msgLabel.Text = upperEncodedMsg;
}
}
}
I'd go down the route of creating a mapping using a Dictionary<char, char>(see here for details on Dictionary) and simply lookup the key value in the dictionary for each char in encodeArray. It may also be better to use a StringBuilder rather than just a string to store your encoded message.
Create Mapping
//Add Dictionary at the top of the class along with upperEncodedMsg string.
private Dictionary<char, char> charMapping;
private StringBuilder upperEncodedMsg;
//Create mappings. Use uppercase values here and you won't need to use your 'upperEncoded' method. This bit can be done in your Form1 constructor.
public Form1()
{
InitializeComponent();
charMapping = new Dictionary<char, char>();
charMapping.Add('A', 'T');
charMapping.Add('B', 'U');
//more mappings...
}
You can then remove the big switch statement you have and replace it with the following:
public void Encode()
{
upperEncodedMsg = new StringBuilder();
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToUpper().ToCharArray();
for(int i = 0; i < encodeArray.Length; i++)
{
//Use the mappings created earlier to get the associated char.
char outputLetter;
charMapping.TryGetValue(encodeArray[i], out outputLetter);
//Append letter to your upperEncodedMsg StringBuilder.
upperEncodedMsg.Append(outputLetter);
}
}
In your button click, at the bottom you can then add:
private void encodeBtn_Click(object sender, EventArgs e)
{
//your code...
msgLabel.Text = upperEncodedMsg.ToString();
}
The main benefit of doing it this way is that your mappings can be made available in other parts of your code. It also makes it easier to maintain and your mappings are loaded as soon as the form is, rather than waiting to call your encode method. A lookup of a Dictionary is also likely to be quicker than a large switch statement.

c# Switch issue. Homework

This is what I have so far. My problem is that none of the cases are responding when you enter either the correct or incorrect answer. I'm not really sure where to go from here. The program asks you answer two random numbers being multiplied. And then it should give you one of the eight responses.
int result = 0;
int caseSwitch = 0;
string question = DoMultiplication(out result);
Console.WriteLine(question);
int answer = Convert.ToInt32(Console.ReadLine());
if (answer == result)
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("Very Good");
break;
case 2:
Console.WriteLine("Excellent");
break;
case 3:
Console.WriteLine("Nice Work");
break;
case 4:
Console.WriteLine("Keep up the good work!");
break;
}
}
else
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("No, Please Try Again.");
break;
case 2:
Console.WriteLine("Wrong, Try Once More");
break;
case 3:
Console.WriteLine("Don't Give Up!");
break;
case 4:
Console.WriteLine("No, Keep Trying!");
break;
caseSwitch is always 0, so your switch will always fall through without writing anything to console.
If you want a random response you could do something like this:
int result = 0;
int caseSwitch = new Random().Next(1, 4);
string question = DoMultiplication(out result);
Console.WriteLine(question);
int answer = Convert.ToInt32(Console.ReadLine());
if (answer == result)
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("Very Good");
break;
case 2:
Console.WriteLine("Excellent");
break;
case 3:
Console.WriteLine("Nice Work");
break;
case 4:
Console.WriteLine("Keep up the good work!");
break;
}
}
else
{
switch (caseSwitch)
{
case 1:
Console.WriteLine("No, Please Try Again.");
break;
case 2:
Console.WriteLine("Wrong, Try Once More");
break;
case 3:
Console.WriteLine("Don't Give Up!");
break;
case 4:
Console.WriteLine("No, Keep Trying!");
break;
CaseSwitch is always = 0.
You need to assign a value to it, and-or add a default case to your switch.
You have your int caseSwitch = 0; and I don't see you changing it in your code to any of 1-4. So what do you expect it to do if you dont have the caseSwitch changed...

Switch case statement

I am trying to create an application in C# that converts numbers in a text box to roman numerals in a label control and need to use a case statement. However one of my variable Roman gets the error message: Use of unassigned local variable 'Roman'.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Roman_Numeral_Converter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalc_Click(object sender, EventArgs e)
{
int Number=int.Parse(txtNum.Text); // To hold Number
string Roman; // To hold Roman Numeral
if (Number>=1 && Number <=10)
{
switch (Roman)
{
case "Number==1":
lblRoman.Text = "I";
break;
case "Number==2":
lblRoman.Text = "II";
break;
case "Number==3":
lblRoman.Text = "III";
break;
case "Number==4":
lblRoman.Text = "IV";
break;
case "Number==5":
lblRoman.Text = "V";
break;
case "Number==6":
lblRoman.Text = "VI";
break;
case "Number==7":
lblRoman.Text = "VII";
break;
case "Number==8":
lblRoman.Text = "VIII";
break;
case "Number==9":
lblRoman.Text = "IX";
break;
case "Number==10":
lblRoman.Text = "X";
break;
}
}
else
{
MessageBox.Show("Error: Invalid Input");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close the form.
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtNum.Text = "";
lblRoman.Text = "";
}
}
}
Your structure is a little off.
private void btnCalc_Click(object sender, EventArgs e)
{
var Number = int.Parse(txtNum.Text); // To hold Number
switch (Number)
{
case 1:
lblRoman.Text = "I";
break;
case 2:
lblRoman.Text = "II";
break;
case 3:
lblRoman.Text = "III";
break;
case 4:
lblRoman.Text = "IV";
break;
case 5:
lblRoman.Text = "V";
break;
case 6:
lblRoman.Text = "VI";
break;
case 7:
lblRoman.Text = "VII";
break;
case 8:
lblRoman.Text = "VIII";
break;
case 9:
lblRoman.Text = "IX";
break;
case 10:
lblRoman.Text = "X";
break;
default:
MessageBox.Show("Error: Invalid Input");
break;
}
}
You're using the lblRoman to hold your result, thus your Roman variable is unnecessary. Additionally, since you're interrogating every possible valid number in your switch, you can just use the default to replace your if/else structure.
I'm assuming you're doing this as an academic exercise. That being said, I would be remiss not to point to you Mosè Bottacini's solution to this problem.
This is because Roman variable is really unassigned. You should assign it before you enter the statement
try this,
when your number value like 1 so roman number is I.
private void btnCalc_Click(object sender, EventArgs e)
{
int Number = int.Parse(txtNum.Text); // To hold Number
string Roman; // To hold Roman Numeral
if (Number >= 1 && Number <= 10)
{
switch (Number)
{
case 1:
lblRoman.Text = "I";
break;
case 2:
lblRoman.Text = "II";
break;
case 3:
lblRoman.Text = "III";
break;
case 4:
lblRoman.Text = "IV";
break;
case 5:
lblRoman.Text = "V";
break;
case 6:
lblRoman.Text = "VI";
break;
case 7:
lblRoman.Text = "VII";
break;
case 8:
lblRoman.Text = "VIII";
break;
case 9:
lblRoman.Text = "IX";
break;
case 10:
lblRoman.Text = "X";
break;
}
}
else
{
MessageBox.Show("Error: Invalid Input");
}
}
Instead of switch, You can do other way using Linq which is even better.
int Number=int.Parse(txtNum.Text);
var romanList = new List<string> {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
if (Number >= 1 && Number <= 10)
lblRoman.Text = romanList.Select((r, i) => new { Roman = r, Index = i+1}).FirstOrDefault(x=> x.Index == Number).Roman;
you can replace your switch statement this way. and of course you need to assign a variable before using it.
public string GetNum(string val)
{
string res = ""; // Assign it an empty string.
var numToRom = new Dictionary<string, string>
{
{"1","I"},
{"2","II"}
//so on
};
numToRom.TryGetValue(val, out res);
return res;
}

Finding the index of chars that repeats

I'm really bad at explaining things, but I'll try my best.
I'm making a small program that converts one word into another as you type. Each letter that is typed goes through this section of code where it is changed to a different letter depending on its Index position of the whole word.
My issue here is that when there are repeating letters, the letters that repeat don't change according to their position within the word but rather the first occurrence.
For example this made up word "bacca". If you put that through the code, it SHOULD change to "vrwiy" but instead it changes to "vrwwr". I know why this is too. It's because the switch statement loops through the word that needs to be converted. However I'm without a clue on how to make it change each char according to it's own individual position within the index of the string. I thought maybe the LastIndexOf() method would work but instead it just reverses the order. So if I were to type the letter "a", it would come out as "n", but if I were to type "aa", it would switch the first "a" to "r" because the second is at the IndexOf 1 get's changed to "r".
private void inputTbox_TextChanged(object sender, EventArgs e)
{
List<string> rawZnWordList = new List<string>();
foreach (char a in inputTextBox.Text)
{
switch (inputTextBox.Text.IndexOf(a))
{
case 0:
switch (a)
{
case 'a':
rawZnWordList.Add("n");
continue;
case 'b':
rawZnWordList.Add("v");
continue;
case 'c':
rawZnWordList.Add("a");
continue;
default:
break;
}
continue;
case 1:
switch (a)
{
case 'a':
rawZnWordList.Add("r");
continue;
case 'b':
rawZnWordList.Add("x");
continue;
case 'c':
rawZnWordList.Add("z");
continue;
default:
break;
}
continue;
case 2:
switch (a)
{
case 'a':
rawZnWordList.Add("t");
continue;
case 'b':
rawZnWordList.Add("l");
continue;
case 'c':
rawZnWordList.Add("w");
continue;
default:
continue;
}
continue;
case 3:
switch (a)
{
case 'a':
rawZnWordList.Add("u");
continue;
case 'b':
rawZnWordList.Add("i");
continue;
case 'c':
rawZnWordList.Add("o");
continue;
default:
break;
}
continue;
case 4:
switch (a)
{
case 'a':
rawZnWordList.Add("y");
continue;
case 'b':
rawZnWordList.Add("m");
continue;
case 'c':
rawZnWordList.Add("d");
continue;
default:
break;
}
continue;
default:
break;
}
}
string finalZnWord = string.Join("", rawZnWordList.ToArray());
outputTextBox.Text = finalZnWord;
}
You should try using a for loop instead, like this:
for (int i = 0; i < inputTextBox.Text.Length; i++)
{
char a = inputTextBox.Text[i];
switch (i)
{
case 0:
switch (a)
...
Hope this helps ;).
You need to keep track of the index inside your foreach instead of using .IndexOf. You could also use a regular for loop instead of a foreach but this way is a minimal change to your code.
private void inputTbox_TextChanged(object sender, EventArgs e)
{
List rawZnWordList = new List();
int index = 0;
foreach (char a in inputTextBox.Text)
{
switch (index)
{
case 0:
switch (a)
{
case 'a':
rawZnWordList.Add("n");
continue;
case 'b':
rawZnWordList.Add("v");
continue;
case 'c':
rawZnWordList.Add("a");
continue;
default:
break;
}
continue;
case 1:
switch (a)
{
case 'a':
rawZnWordList.Add("r");
continue;
case 'b':
rawZnWordList.Add("x");
continue;
case 'c':
rawZnWordList.Add("z");
continue;
default:
break;
}
continue;
case 2:
switch (a)
{
case 'a':
rawZnWordList.Add("t");
continue;
case 'b':
rawZnWordList.Add("l");
continue;
case 'c':
rawZnWordList.Add("w");
continue;
default:
continue;
}
continue;
case 3:
switch (a)
{
case 'a':
rawZnWordList.Add("u");
continue;
case 'b':
rawZnWordList.Add("i");
continue;
case 'c':
rawZnWordList.Add("o");
continue;
default:
break;
}
continue;
case 4:
switch (a)
{
case 'a':
rawZnWordList.Add("y");
continue;
case 'b':
rawZnWordList.Add("m");
continue;
case 'c':
rawZnWordList.Add("d");
continue;
default:
break;
}
continue;
default:
break;
}
index++;
}
string finalZnWord = string.Join("", rawZnWordList.ToArray());
outputTextBox.Text = finalZnWord;
}
I think this does the same thing and is a lot more readable. Of course replace the letter rings with your own values. I only went up to 5 characters. I'm guessing you would want more.
//replacement letter rings
char[][] aRep = {
"xfhygaodsekzcpubitlvnjqmrw".ToCharArray(),
"wqtnsepkbalmzyxvordhjgifcu".ToCharArray(),
"nyxgmcibplovkwrszaehftqjud".ToCharArray(),
"soqjhpybuwfxvartkzginemdcl".ToCharArray(),
"pldquhegkaomcnjrfxiysvtbwz".ToCharArray(),
};
private string newText(string inVal)
{
char[] ia = inVal.ToCharArray(); //in array
int l = ia.Length;
char[] oa = new char[l]; //out array
for (int i = 0; i < l; i++)
oa[i] = aRep[i][(int)ia[i]-97]; //lowercase starts at char97
return new string(oa);
}
Here is also the code I used to generate the rings:
//generate random letter rings
//char[] ascii = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
//for (int i = 0; i < 8; i++)
// new string(ascii.OrderBy (x => Guid.NewGuid() ).ToArray()).Dump();
I did some very simple testing and this seemed quite a bit faster than the original approach and more importantly (to me) it is way more readable and a lot easier to see your replacement letter sequences. I think there is some more optimization to be done, but this i think is a good start. Just call this with your text during 'on change'.

C# Console Application - Odd behaviour - char '\a'

After extensive debugging of an application, I noticed the console window would hang when searching text for the char '\a'. The goal is to strip out characters from a file. The below portion just prints the stripped output. It causes the same issue.
The console window would always hang upon exiting the program, and it would make it to the last statement of main. I removed the '\a' from the switch statement and the console application does not hang anymore. Any idea why? I still need to strip out the char '\a', but cannot get the application to work without hanging.
static void Main(string[] args)
{
string s_filename = args[0];
Read(s_filename, 0);
}
static void Read(string s_filename, int i_char)
{
try
{
char ch;
StringBuilder sb = new StringBuilder();
using (FileStream fs = new FileStream(s_filename, FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
for (int i = 0; i < fs.Length; i++)
{
byte b_byte = br.ReadByte(); //Reads the bytes one at a time.
ch = (char)(b_byte);
if (isString(ch, i_char) || (sb.Length > 0 && ch == ' '))
sb.Append(ch);
else
{
if (sb.Length == 0)
continue;
if (sb.Length >= 4)
{
Console.WriteLine(sb);
}
sb.Length = 0;
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Error {0}", e);
}
}
static bool isString(char c, int i) //http://msdn.microsoft.com/en-us/library/h21280bw.aspx
{
if (i == 0)
{
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
return true;
if (c >= '0' && c <= '9')
return true;
switch (c)
{
case '~':
case '`':
case '!':
case '#':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':
case '-':
case '_':
case '+':
case '=':
case '[':
case ']':
case '{':
case '}':
case '|':
case '\\':
case ';':
case ':':
case '"':
case '\'':
case '<':
case '>':
case ',':
case '.':
case '?':
case '/':
case '\t': //Horizontal Tab
case '\v': //Vertical Tab
case '\n': //Newline
case '\f'://Formfeed
case '\r': //carriage return
case '\b': //Backspace
case '\x7f': //delete character
case '\x99': //TM Trademark
case '\a': //Bell Alert
return true;
}
}
if (i == 1)
{
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
return true;
if (c >= '0' && c <= '9')
return true;
switch (c)
{
case '~':
case '`':
case '!':
case '#':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':
case '-':
case '_':
case '+':
case '=':
case '[':
case ']':
case '{':
case '}':
case '|':
case '\\':
case ';':
case ':':
case '"':
case '\'':
case '<':
case '>':
case ',':
case '.':
case '?':
case '/':
return true;
}
}
if (i == 2)
{
if (Char.IsLetterOrDigit(c))
return true;
switch (c)
{
case '~':
case '`':
case '!':
case '#':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':
case '-':
case '_':
case '+':
case '=':
case '[':
case ']':
case '{':
case '}':
case '|':
case '\\':
case ';':
case ':':
case '"':
case '\'':
case '<':
case '>':
case ',':
case '.':
case '?':
case '/':
return true;
}
}
if (i == 3)
{
if (Char.IsLetterOrDigit(c))
return true;
switch (c)
{
case '~':
case '`':
case '!':
case '#':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':
case '-':
case '_':
case '+':
case '=':
case '[':
case ']':
case '{':
case '}':
case '|':
case '\\':
case ';':
case ':':
case '"':
case '\'':
case '<':
case '>':
case ',':
case '.':
case '?':
case '/':
case '\t': //Horizontal Tab
case '\v': //Vertical Tab
case '\n': //Newline
case '\f'://Formfeed
case '\r': //carriage return
case '\b': //Backspace
case '\x7f': //delete character
case '\x99': //TM Trademark
case '\a': //Bell Alert
return true;
}
}
if (i == 4)
{
if (Char.IsLetterOrDigit(c))
return true;
}
if (i == 5)
{
if (c >= 'a' && c <= 'z')
return true;
if (c >= 'A' && c <= 'Z')
return true;
if (c >= '0' && c <= '9')
return true;
}
return false;
}
I don't see why your program would hang, but you haven't given us much to go on.
Maybe try replacing the '\a' with a literal 7.
See also http://asciitable.com/ for other character codes.
But I'm thinking it has to do with some other program logic, not just the '\a' in your code.
Anyway, that seems to me a better approach - try to rework your code if applicable:
using System.Linq;
bool Contains(string input)
{
var arr = new[] { '\t', '\v', '\n', '\f', '\r', '\b', '\x7f', '\x99', '\a', .. };
return arr.Any(c => input.Contains(c));
}

Categories

Resources