String Reversal plus character replacement
-
Hello, I'm currently in the midst of taking an assessment test, and I need some help with something. essentially, what I need to do, is dynamically reverse a string as it is entered into a text box, and while it is being reversed, replace every number entered (1,2,3) with it's word equivalent (one,two,three). Now I have the string reversal part nailed with the following function:
public string Reverse(string x) { char[] charArray = new char[x.Length]; int len = x.Length - 1; for (int i = 0; i <= len; i++) charArray[i] = x[len-i]; return new string(charArray); }
My main problem is I am not very sure how to dynamically change the numbers to words as the entered string is being reversed. I am considering using some code along the lines of this:public string numCheck(string x) { string[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; string[] words = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; string strReplaced = ""; int i; for(i=0; i9) { strReplaced = x.Replace(numbers[i], words[i]); x = x.Replace(ch); } return x; }
However, I do not know how to properly insert this into the rest of my program to get the desired result, or even if this is the best way to do it. Thank you in advance. -
Hello, I'm currently in the midst of taking an assessment test, and I need some help with something. essentially, what I need to do, is dynamically reverse a string as it is entered into a text box, and while it is being reversed, replace every number entered (1,2,3) with it's word equivalent (one,two,three). Now I have the string reversal part nailed with the following function:
public string Reverse(string x) { char[] charArray = new char[x.Length]; int len = x.Length - 1; for (int i = 0; i <= len; i++) charArray[i] = x[len-i]; return new string(charArray); }
My main problem is I am not very sure how to dynamically change the numbers to words as the entered string is being reversed. I am considering using some code along the lines of this:public string numCheck(string x) { string[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; string[] words = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; string strReplaced = ""; int i; for(i=0; i9) { strReplaced = x.Replace(numbers[i], words[i]); x = x.Replace(ch); } return x; }
However, I do not know how to properly insert this into the rest of my program to get the desired result, or even if this is the best way to do it. Thank you in advance.You need to include some code in the loop of your main function that checks whether the current character is numeric. If it is, you need to do a lookup to find the word that represents this number and replace the number with its word equivalent.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
You need to include some code in the loop of your main function that checks whether the current character is numeric. If it is, you need to do a lookup to find the word that represents this number and replace the number with its word equivalent.
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
Lookup? As in a select case statement? so far after taking your advice I have the following:
public string Reverse(string x) { char[] charArray = new char[x.Length]; int len = x.Length - 1; for (int i = 0; i <= len; i++) if(char.IsNumber()) { } charArray[i] = x[len-i]; return new string(charArray); }
I suppose my next dilemma is trying to figure out what goes into that if statement, I believe my best bet is some sort of select case statement, though the last time I tried that resulted in error. And I'm still trying to figure out what to about the IsNumber(), or rather what I need to put between those parenthesis. -
Lookup? As in a select case statement? so far after taking your advice I have the following:
public string Reverse(string x) { char[] charArray = new char[x.Length]; int len = x.Length - 1; for (int i = 0; i <= len; i++) if(char.IsNumber()) { } charArray[i] = x[len-i]; return new string(charArray); }
I suppose my next dilemma is trying to figure out what goes into that if statement, I believe my best bet is some sort of select case statement, though the last time I tried that resulted in error. And I'm still trying to figure out what to about the IsNumber(), or rather what I need to put between those parenthesis.BREdwards wrote:
Lookup? As in a select case statement?
Select...Case
. Array lookup. Hashtable lookup. These are all viable options.BREdwards wrote:
I suppose my next dilemma is trying to figure out what goes into that if statement, I believe my best bet is some sort of select case statement, though the last time I tried that resulted in error. And I'm still trying to figure out what to about the IsNumber(), or rather what I need to put between those parenthesis.
Keep trying and you'll get there. Good luck :).
Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
Hello, I'm currently in the midst of taking an assessment test, and I need some help with something. essentially, what I need to do, is dynamically reverse a string as it is entered into a text box, and while it is being reversed, replace every number entered (1,2,3) with it's word equivalent (one,two,three). Now I have the string reversal part nailed with the following function:
public string Reverse(string x) { char[] charArray = new char[x.Length]; int len = x.Length - 1; for (int i = 0; i <= len; i++) charArray[i] = x[len-i]; return new string(charArray); }
My main problem is I am not very sure how to dynamically change the numbers to words as the entered string is being reversed. I am considering using some code along the lines of this:public string numCheck(string x) { string[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; string[] words = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; string strReplaced = ""; int i; for(i=0; i9) { strReplaced = x.Replace(numbers[i], words[i]); x = x.Replace(ch); } return x; }
However, I do not know how to properly insert this into the rest of my program to get the desired result, or even if this is the best way to do it. Thank you in advance.First off, I'd say this should be in the KeyPress event handler. The replacements can be done with a switch or Dictionary. I expect the quickest way to do the reversal is to set the insertion point back to the start of the text with
SelectionStart=0
after each character is added, but the user could move the insertion point so handle Click as well. No, wait, setRightToLeft=true
:-D -
First off, I'd say this should be in the KeyPress event handler. The replacements can be done with a switch or Dictionary. I expect the quickest way to do the reversal is to set the insertion point back to the start of the text with
SelectionStart=0
after each character is added, but the user could move the insertion point so handle Click as well. No, wait, setRightToLeft=true
:-DThanks so far. The string reversal part is all taken care of, my problem is having to dynamically replace all number with their word equivalents, here's what I have so far:
public string Reverse(string x) { int i; char[] charArray = new char[x.Length]; int len = x.Length - 1; for ( i = 0; i <= len; i++) if(char.IsNumber(x,i)) { switch(i) { case'0': x.Replace("0","zero"); break; } } charArray[i] = x[len-i]; return new string(charArray); }
I have something of an idea of what needs to be done, problem is, I can't seem to use that if statement without breaking the program. I keep getting an "index was outside the bounds of the array error" for both letters and numbers. I simply don't understand what I'm doing wrong, never had to do something like this before. -
Thanks so far. The string reversal part is all taken care of, my problem is having to dynamically replace all number with their word equivalents, here's what I have so far:
public string Reverse(string x) { int i; char[] charArray = new char[x.Length]; int len = x.Length - 1; for ( i = 0; i <= len; i++) if(char.IsNumber(x,i)) { switch(i) { case'0': x.Replace("0","zero"); break; } } charArray[i] = x[len-i]; return new string(charArray); }
I have something of an idea of what needs to be done, problem is, I can't seem to use that if statement without breaking the program. I keep getting an "index was outside the bounds of the array error" for both letters and numbers. I simply don't understand what I'm doing wrong, never had to do something like this before. -
Thanks so far. The string reversal part is all taken care of, my problem is having to dynamically replace all number with their word equivalents, here's what I have so far:
public string Reverse(string x) { int i; char[] charArray = new char[x.Length]; int len = x.Length - 1; for ( i = 0; i <= len; i++) if(char.IsNumber(x,i)) { switch(i) { case'0': x.Replace("0","zero"); break; } } charArray[i] = x[len-i]; return new string(charArray); }
I have something of an idea of what needs to be done, problem is, I can't seem to use that if statement without breaking the program. I keep getting an "index was outside the bounds of the array error" for both letters and numbers. I simply don't understand what I'm doing wrong, never had to do something like this before.This does what you ask using a Hashtable...
public string Reverse(string x)
{
char[] charArray = x.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}private static class NumericStringRepresentations { private static Hashtable \_PairsTable = new Hashtable(9); public static Hashtable PairsTable { get { return \_PairsTable; } } static NumericStringRepresentations() { \_PairsTable.Add("0", "zero"); \_PairsTable.Add("1", "one"); \_PairsTable.Add("2", "two"); \_PairsTable.Add("3", "three"); \_PairsTable.Add("4", "four"); \_PairsTable.Add("5", "five"); \_PairsTable.Add("6", "six"); \_PairsTable.Add("7", "seven"); \_PairsTable.Add("8", "eight"); \_PairsTable.Add("9", "nine"); } } public string NumbersToStrings(string x) { string returnString = ""; if (!(x.Length == 0)) { for (int i = 0; i < x.Length; i++) { string thisCharacter = x.Substring(i, 1); int returnedInt; if (int.TryParse(thisCharacter, out returnedInt)) { returnString += NumericStringRepresentations.PairsTable\[thisCharacter\]; } else { returnString += thisCharacter; } } } return returnString; }
-
This does what you ask using a Hashtable...
public string Reverse(string x)
{
char[] charArray = x.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}private static class NumericStringRepresentations { private static Hashtable \_PairsTable = new Hashtable(9); public static Hashtable PairsTable { get { return \_PairsTable; } } static NumericStringRepresentations() { \_PairsTable.Add("0", "zero"); \_PairsTable.Add("1", "one"); \_PairsTable.Add("2", "two"); \_PairsTable.Add("3", "three"); \_PairsTable.Add("4", "four"); \_PairsTable.Add("5", "five"); \_PairsTable.Add("6", "six"); \_PairsTable.Add("7", "seven"); \_PairsTable.Add("8", "eight"); \_PairsTable.Add("9", "nine"); } } public string NumbersToStrings(string x) { string returnString = ""; if (!(x.Length == 0)) { for (int i = 0; i < x.Length; i++) { string thisCharacter = x.Substring(i, 1); int returnedInt; if (int.TryParse(thisCharacter, out returnedInt)) { returnString += NumericStringRepresentations.PairsTable\[thisCharacter\]; } else { returnString += thisCharacter; } } } return returnString; }
Marvelous! I was planning to use an array, but this works as well. Thank you, I'll be sure to save this so I can study later. However, when compiled, it delivers an error saying: "The Modifier 'static' is not valid for this term." After a bit of research, I found that my version of VS is too old to have static as a modifier for the class, as I am using 2003 still. Anyhoo, thanks for eveything.