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'.
Related
UPDATED:
Is there a way to accomplish what the code below attempts to do (doesn't pass syntax because case 2 and case 3 can't have duplicate declarations and/or because num can't be 2 and 3 at the same time)?
var num = 0;
switch (num)
{
case 1:
//do stuff applicable to 1 only;
break;
case 2:
//do stuff applicable to 2 only;
break;
case 3:
//do stuff applicable to 3 only;
break;
case 2:
case 3:
//do stuff applicable to 2 and 3 only;
break;
}
The question is confusingly posed. I think what you want is to have a case that runs for two, a case that runs for three, and a case that runs for two-or-three.
Just use two switches:
switch (num)
{
case 1: One(); break;
case 2: Two(); break;
case 3: Three(); break;
}
switch (num)
{
case 2:
case 3: TwoOrThree(); break;
}
Or
switch (num)
{
case 1: One(); break;
case 2:
case 3:
switch (num)
{
case 2: Two(); break;
case 3: Three(); break;
}
TwoOrThree();
break;
}
Or duplicate the code:
switch (num)
{
case 1: One(); break;
case 2: Two(); TwoOrThree(); break;
case 3: Three(); TwoOrThree(); break;
}
I would NOT recommend un-duplicating the code like this:
switch (num)
{
case 1: One(); break;
case 2: Two(); goto twoOrThree; break;
case 3: Three(); twoOrThree: TwoOrThree(); break;
}
Yuck.
not with a single-level switch (or a nasty GOTO). A switch can only execute one case. A better solution would be a nested if (or switch) within the 2/3 case:
switch (num)
{
case 1:
//do stuff applicable to 1 only
break;
case 2:
case 3:
if(num == 2)
{
//do stuff applicable to 2 only
}
if(num == 3)
{
//do stuff applicable to 3 only
}
//do stuff applicable to 2 OR 3
break;
}
It's as simple as this...
switch (value)
{
case 1:
// Do 1 stuff
break;
case 2:
case 3:
// We can use an if-else construct here given that there's only two possibilities.
if (value == 2)
{
// 2 only stuff here
}
else
{
// 3 only stuff here
}
// Do anything applicable to BOTH here, or above the if construct, depending on your requirements.
break;
default:
// Any other stuff here
break;
}
Or just use an if construct, which is arguably cleaner (switch is really only intended for simple many to one logical mappings)...
if (value == 1)
{
// 1 stuff
}
else if (value == 2 || value == 3)
{
if (value == 2) {
// 2 stuff only
}
else
{
// 3 stuff only
}
// 2 or 3 stuff, or above the if construct above, if you require.
}
else
{
// Anything else here
}
Or simply...
switch (value)
{
case 1:
OneStuff();
break;
case 2:
TwoStuff();
TwoOrThreeStuff();
break;
case 3:
ThreeStuff();
TwoOrThreeStuff();
break;
default:
AnythingElse();
}
And wrap what you need to do for each of the above in separate methods.
The third method would be my preference here. It's cleaner and easier to debug than using complex mixtures of switch and if.
You could just use if statements instead
var num = 0;
if(num == 1)
{
// do stuff applicable to 1 only
}
else if(num == 2)
{
// do stuff applicable to 2 only
}
else if(num == 3)
{
// do stuff applicable to 3 only
}
if(num == 2 || num == 3)
{
// do stuff applicable to 2 AND 3
}
Or as a switch and an if
switch (num)
{
case 1:
// do stuff applicable to 1 only
break;
case 2:
// do stuff applicable to 2 only
break;
case 3:
// do stuff applicable to 3 only
break;
}
if(num == 2 || num == 3)
{
// do stuff applicable to 2 AND 3
}
You could use the goto case statement and have a unique identifier for combined operations in case 2 and 3. Make sure that that is never hit by any other number:
var num = 0;
var text = "";
switch (num)
{
case 1:
text = "do stuff applicable to 1 only";
break;
case 2:
text = "do stuff applicable to 2 only";
goto case 23;
case 3:
text = "do stuff applicable to 3 only";
goto case 23;
case 23:
text = "do stuff applicable to 2 AND 3";
break;
}
The use of goto is not recommended in general but it is common to use it in switch-statements and situations like that.
I have a program that generates C# from bits of C# stored in an XML file. If I have a snippet like:
foo {bar}
I need to transform that into an interpolated string, like this:
$#"foo {bar}"
The problem is that, if I have quotes outside a placeholder, e.g.:
"foo" {bar}
I need to double those:
$#"""foo"" {bar}"
but ignore quotes inside placeholders:
foo {"bar"}
should produce:
$#"foo {"bar"}"
Also, need to look out for doubled braces:
foo {{"bar"}}
should produce:
$#"foo {{""bar""}}"
And perhaps the trickiest of all, if the placeholder is preceded and/or followed by an even number of braces:
foo {{{"bar"}}}
should produce:
$#"foo {{{"bar"}}}"
In short, if there's a placeholder then ignore everything inside. For the rest of the text, double quotes.
Can this be accomplished using regular expressions? If not, what alternatives do I have?
You will need at least 2 steps:
Add quotes inside the expression:
"(?=[^}]*(?:}})*[^}]*$)|(?<=^[^{]*(?:{{)*)" =>
replace with ""
See demo
Enclose in $#"..." with string.Format("$#\"{0}\"", str);
Here is an IDEONE demo
var s = "\"foo\" {bar}";
var rx = new Regex(#"(?<!(?<!{){[^{}]*)""(?![^{}]*}(?!}))");
Console.WriteLine(string.Format("$#\"{0}\"",rx.Replace(s,"\"\"")));
And another demo here
This cannot be done with regular expressions. Knowing when a placeholder starts is easy, knowing when it ends is the hard part, since a placeholder can hold almost any C# expression, so you have to keep track of blocks ({}) and literals (strings, chars, comments) because any brace in a literal is not significant.
This is the code I came up with:
enum ParsingMode {
Text,
Code,
InterpolatedString,
InterpolatedVerbatimString,
String,
VerbatimString,
Char,
MultilineComment
}
public static string EscapeValueTemplate(string valueTemplate) {
if (valueTemplate == null) throw new ArgumentNullException(nameof(valueTemplate));
var quoteIndexes = new List<int>();
var modeStack = new Stack<ParsingMode>();
modeStack.Push(ParsingMode.Text);
Func<ParsingMode> currentMode = () => modeStack.Peek();
for (int i = 0; i < valueTemplate.Length; i++) {
char c = valueTemplate[i];
Func<char?> nextChar = () =>
i + 1 < valueTemplate.Length ? valueTemplate[i + 1]
: default(char?);
switch (currentMode()) {
case ParsingMode.Code:
switch (c) {
case '{':
modeStack.Push(ParsingMode.Code);
break;
case '}':
modeStack.Pop();
break;
case '\'':
modeStack.Push(ParsingMode.Char);
break;
case '"':
ParsingMode stringMode = ParsingMode.String;
switch (valueTemplate[i - 1]) {
case '#':
if (i - 2 >= 0 && valueTemplate[i - 2] == '$') {
stringMode = ParsingMode.InterpolatedVerbatimString;
} else {
stringMode = ParsingMode.VerbatimString;
}
break;
case '$':
stringMode = ParsingMode.InterpolatedString;
break;
}
modeStack.Push(stringMode);
break;
case '/':
if (nextChar() == '*') {
modeStack.Push(ParsingMode.MultilineComment);
i++;
}
break;
}
break;
case ParsingMode.Text:
case ParsingMode.InterpolatedString:
case ParsingMode.InterpolatedVerbatimString:
switch (c) {
case '{':
if (nextChar() == '{') {
i++;
} else {
modeStack.Push(ParsingMode.Code);
}
break;
case '"':
switch (currentMode()) {
case ParsingMode.Text:
quoteIndexes.Add(i);
break;
case ParsingMode.InterpolatedString:
modeStack.Pop();
break;
case ParsingMode.InterpolatedVerbatimString:
if (nextChar() == '"') {
i++;
} else {
modeStack.Pop();
}
break;
}
break;
case '\\':
if (currentMode() == ParsingMode.InterpolatedString) {
i++;
}
break;
}
break;
case ParsingMode.String:
switch (c) {
case '\\':
i++;
break;
case '"':
modeStack.Pop();
break;
}
break;
case ParsingMode.VerbatimString:
if (c == '"') {
if (nextChar() == '"') {
i++;
} else {
modeStack.Pop();
}
}
break;
case ParsingMode.Char:
switch (c) {
case '\\':
i++;
break;
case '\'':
modeStack.Pop();
break;
}
break;
case ParsingMode.MultilineComment:
if (c == '*') {
if (nextChar() == '/') {
modeStack.Pop();
i++;
}
}
break;
}
}
var sb = new StringBuilder(valueTemplate, valueTemplate.Length + quoteIndexes.Count);
for (int i = 0; i < quoteIndexes.Count; i++) {
sb.Insert(quoteIndexes[i] + i, '"');
}
return sb.ToString();
}
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...
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);
}
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));
}