2014/04/18

String Manipulation by detecting each character

Programming Language: C#.net

Need to manipulate or replace strings that matches the criteria? Or do you have a complex problem that involves a string of characters?


In today's lesson, we'll exercise our knowledge in String Manipulation. We'll use "Your Japanese name equivalent" from the image you see above as an example in our topic for today.

So I'm going to show you a string conversion of your name in Japanese.


But before that, I'll assume that you possess the following prerequisites for this exercise:
• Basic knowledge of C#.net or VB.net
• Basic knowledge of OOP


Step 1: Create a new Console Application. Name it as NameConversionToJapanese


Step 2: Paste this code snippet within your Main method
            
Console.Write("Please enter your name: ");
var MyName = Console.ReadLine();
Console.Clear();
Console.WriteLine("Your equivalent name in Japanese: " + ConvertingStringProcedure(MyName));

From the code snippet I've provided, the MyName variable gets the name you inputted. And ConvertingStringProcedure(string mystring) constructor will help you to convert the following input to the output you desired. In this lesson, we are converting our name into corresponding Japanese name. I'll delve deeper into the constructor and where you should pay attention in Step 3.


Step 3: Paste this helper method within your class
private static string ConvertingStringProcedure(string mystring)
        {
            var ConvertedString = "";
            var myLoop = 0;
            while (myLoop < mystring.Length)
            {
                switch (mystring.Substring(myLoop, 1).ToLower()) //use substring to evaluate every character in a set of words
                {
                    case "a":
                        ConvertedString += "ka"; //use concatenation by putting += to the variable
                        break;
                    case "b":
                        ConvertedString += "zu";
                        break;
                    case "c":
                        ConvertedString += "mi";
                        break;
                    case "d":
                        ConvertedString += "te";
                        break;
                    case "e":
                        ConvertedString += "ku";
                        break;
                    case "f":
                        ConvertedString += "lu";
                        break;
                    case "g":
                        ConvertedString += "ji";
                        break;
                    case "h":
                        ConvertedString += "ri";
                        break;
                    case "i":
                        ConvertedString += "ki";
                        break;
                    case "j":
                        ConvertedString += "zu";
                        break;
                    case "k":
                        ConvertedString += "me";
                        break;
                    case "l":
                        ConvertedString += "ta";
                        break;
                    case "m":
                        ConvertedString += "rin";
                        break;
                    case "n":
                        ConvertedString += "to";
                        break;
                    case "o":
                        ConvertedString += "mo";
                        break;
                    case "p":
                        ConvertedString += "no";
                        break;
                    case "q":
                        ConvertedString += "ke";
                        break;
                    case "r":
                        ConvertedString += "shi";
                        break;
                    case "s":
                        ConvertedString += "ari";
                        break;
                    case "t":
                        ConvertedString += "chi";
                        break;
                    case "u":
                        ConvertedString += "do";
                        break;
                    case "v":
                        ConvertedString += "ru";
                        break;
                    case "w":
                        ConvertedString += "wei";
                        break;
                    case "x":
                        ConvertedString += "na";
                        break;
                    case "y":
                        ConvertedString += "fu";
                        break;
                    case "z":
                        ConvertedString += "zi";
                        break;
                    default:
                        ConvertedString += " ";
                        break;
                }
                myLoop++;
            }
            return ConvertedString.Substring(0,1).ToUpper() + ConvertedString.Substring(1,ConvertedString.Length - 1);
        }

From the code snippet I've provided, we have used a while loop to iterate each character within your input and a switch-case for conditions. And that's not it. These won't work if you don't have the means of filtering each character whether they meet certain requirements and that's where the Substring will take into play. Substring method within string datatype will handle character-specific scenarios for you. You can also use this when there are things within the string which you don't want to display depending on the situation. It is not limited to one character, in fact you could output as many characters as you like.

To give you a better understanding of Substring, let me show you some of the examples associated with it:

Ex.

"helloworld".Substring(0,5); //will give you an output of "hello".
"helloworld".Substring(1,5); //will give you an output of "ellow".
"helloworld".Substring(5,5); //will give you an output of "world".

This is because the first parameter within Substring specifies an index value where the Substring will start evaluating. And second parameter specifies how many characters you wanted to display starting from the index value you specified.

Now, from the picture above I've encircled, I put the loop value within the first parameter to make sure each character is evaluated until the end of loop and set the second parameter to 1 so that code could focus only to the current value of index and character which needs evaluation. I added ToLower method to avoid unnecessary character conflict between upper case and lower case. Then, add += to your variable that will show converted characters, this way each character conversion can be concatenated in each loop iteration.

Output:



Method 2 

Although we have achieved our objectives for today, you might want to have a shorter and cleaner code. Don't worry, they still yield the same output.

In method 2, we'll replace switch-case that was used to fetch corresponding characters using Dictionary<>  for more shorter code.




Step 1: Create a Class. Name it JapaneseCharacters



Step 2: Create a Dictionary<> and name it _LibraryOfJapaneseCharacters

After creating a dictionary<>, your code should like this:
//create a generic

private static readonly Dictionary<string, string> _LibraryOfJapaneseCharacters = new Dictionary<string, string>
        {
            {"a","ka"},
            {"b","zu"},
            {"c","mi"},
            {"d","te"},
            {"e","ku"},
            {"f","lu"},
            {"g","ji"},
            {"h","ri"},
            {"i","ki"},
            {"j","zu"},
            {"k","me"},
            {"l","ta"},
            {"m","rin"},
            {"n","to"},
            {"o","mo"},
            {"p","no"},
            {"q","ke"},
            {"r","shi"},
            {"s","ari"},
            {"t","chi"},
            {"u","do"},
            {"v","ru"},
            {"w","mei"},
            {"x","na"},
            {"y","fu"},
            {"z","zi"},
        };

Step 3: Paste this code snippet within JapaneseCharacter class

//converting characters static method
private static string myConvertedCharacters = "";
        internal static string GetCharacterInput(string myInput)
        {
            var result = "";
            var myLoop = 0;
            while (myLoop < myInput.Length)
            {
                _LibraryOfJapaneseCharacters.TryGetValue(myInput.Substring(myLoop, 1).ToLower(), out result);
                if (result == null)
                    myConvertedCharacters += " ";
                else
                    myConvertedCharacters += result;
                myLoop++;
            }
            return myConvertedCharacters;
        }

Step 4: Call GetCharacterInput(*your input*) within JapaneseCharacter class

it should look like this:

//your output
Console.WriteLine("\n\nYour equivalent name in Japanese: " + JapaneseCharacters.GetCharacterInput(MyName));

Notice that using Dictionary<> is a better approach than using switch-case statement since we're dealing with a collection of characters.

And that concludes the lesson. Hope you learned something.


P.S. If you have questions, suggestions/critics to improve my code more, don't be shy to drop some comments. Alternatively, you can email me at reciosonny@gmail.com for any questions or suggestions.

2 comments:

Unknown said...

Brilliant tutorial and now have managed to convert it into c++ using Maps.

Unknown said...

thanks if you've appreciate it. :D