Enumerating Chars In a String And Checking Odds Or Even Numbers
I will make this a very short blog on the subject of enumerating characters in a string and checking if they are numeric, and also to check if that number is odd or even. We will be doing this with a console application. Let start by asking our user to provide us with a four digit number :
Console.WriteLine("Write a numeric value of four digits :"); string integer = Console.ReadLine();
Console.Readline reads the integral value into a string variable named integer.
string output = string.Empty;
Declare a string named output. We will use this towards the end. Using a using statement as follows below, we can ensure the resources (no matter how small are disposed of by using a using statement.)
using (CharEnumerator char_Value = integer.GetEnumerator())
The enumerator has a method called MoveNext() which indicates with a bool value whether or not the enumerator moved to the next entry or item in the enumeration cycle.
while (char_Value.MoveNext())
Next, we will use int.TryParse and parse the char_Value.Current to String() because the int.TryParse() method takes a string value in order to parse a value. If the parse is successful, we out the value as an integer and this integer : converted_To_Integer
we will use for the rest of the enumeration.
if (int.TryParse(char_Value.Current.ToString(), out int converted_To_Integer))
Inside the if statement we will issue another if statement to check if the number we cast is odd or even.
if (Int_Is_Odd_Or_Even(converted_To_Integer))
In this statement we are calling on a function to return a result of true or false for the converted_To_Integral value which we parsed in the step before. And that function looks like this :
public static bool Int_Is_Odd_Or_Even(int value) { if (value % 2 == 0) { return true; /* Even */ } else { return false; /* is odd */ } }
Taking note, that the function takes one parameter value in the form of an integer. Int_Is_Odd_Or_Even(int value)
– So when we do this : Int_Is_Odd_Or_Even(converted_To_Integer)
– we are passing in this integer to the function to return a bool value indicating if it is odd or even. If it returns true, its an even number. False indicates an odd number.
So if it is an even number, we will then write to the console :
if (Int_Is_Odd_Or_Even(converted_To_Integer)) { Console.WriteLine("EVEN"); output = string.Concat(output, converted_To_Integer); }
Note that the second part of this iteration using the enumeration while its cycling will continue to combine the numbers starting with the output string variable we declared earlier, and this is then joined with the converted_To_Integer value if indeed it it an even number. Otherwise :
else { Console.WriteLine("ODD"); output = string.Concat(output, converted_To_Integer); }
We will use our else condition of our if statement to return to write to the console and report that the variable number is odd, and also join the output variable with the converted_To_Integer
variable by way of string concatenation.
And our other earlier if statement contains an else clause should the parse fail, and this will throw an exception of FormatException for when the inputted value provided by our user is non numeric. And it will show which character was not parsed.
else { throw new FormatException($"Failed to Parse value. { char_Value.Current.ToString() }"); }
Since we now know that we have parsed the output properly, we can then use Convert.ToInt32 to convert the value to an integral variable now that its been properly parsed and checked :
int full_Number = Convert.ToInt32(output); Console.WriteLine(full_Number);
By the time you have built up the structure, you should have something that looks like this tested and working code :
using System; using System.Runtime.InteropServices; using System.Threading; namespace TestConsoleApp { internal static class Program { internal static void Main() => new Thread(() => Request_Response()) { Name = "Executor" }.Start(); internal static void Request_Response() { Console.WriteLine("Write a numeric value of four digits :"); string integer = Console.ReadLine(); string output = string.Empty; using (CharEnumerator char_Value = integer.GetEnumerator()) { while (char_Value.MoveNext()) { if (int.TryParse(char_Value.Current.ToString(), out int converted_To_Integer)) { if (Int_Is_Odd_Or_Even(converted_To_Integer)) { Console.WriteLine("EVEN"); output = string.Concat(output, converted_To_Integer); } else { Console.WriteLine("ODD"); output = string.Concat(output, converted_To_Integer); } } else { throw new FormatException($"Failed to Parse value. { char_Value.Current.ToString() }"); } } } int full_Number = Convert.ToInt32(output); Console.WriteLine(full_Number); } public static bool Int_Is_Odd_Or_Even(int value) { if (value % 2 == 0) { return true; /* Even */ } else { return false; /* is odd */ } } } }
Summarizing on the section responsible for the calling code. I always use threads when I am working with information and I do this as a matter of good practice. Just because its a console app doesn’t mean you can’t or shouldn’t run your work in a new thread.
The reason I do it is because it’s good practice to follow when working with forms or WPF or any other UI driven application which requires no work actually takes place on the UI thread, and that makes it a good principle to follow in my book. As It’s a good habit to develop when writing code.
internal static void Main() => new Thread(() => Request_Response()) { Name = "Executor" }.Start(); internal static void Request_Response()
The above code executes the Main method and thus responsible for executing the code in the Request_Response method on a bran new thread.