Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String Reversal plus character replacement

String Reversal plus character replacement

Scheduled Pinned Locked Moved C#
helpjsontutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BREdwards
    wrote on last edited by
    #1

    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.

    P P 2 Replies Last reply
    0
    • B BREdwards

      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.

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • P pmarfleet

        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

        B Offline
        B Offline
        BREdwards
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • B BREdwards

          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.

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • B BREdwards

            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.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            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, set RightToLeft=true :-D

            B 1 Reply Last reply
            0
            • P PIEBALDconsult

              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, set RightToLeft=true :-D

              B Offline
              B Offline
              BREdwards
              wrote on last edited by
              #6

              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.

              D 2 Replies Last reply
              0
              • B BREdwards

                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.

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                Try this for the reversal (not taken care of if you have an error message ;) ):

                public string Reverse(string x)
                {
                char[] charArray = x.ToCharArray();
                Array.Reverse(charArray);
                return new string(charArray);
                }

                1 Reply Last reply
                0
                • B BREdwards

                  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.

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  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;
                      }
                  
                  B 1 Reply Last reply
                  0
                  • D DaveyM69

                    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;
                        }
                    
                    B Offline
                    B Offline
                    BREdwards
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups