How do i split a text 2 by 2? - c#

The variable is: Hello
The result I want:
split[0] = he;
split[1] = ll;
split[2] = o + ( space );
I tried this code:
string[] split = new string[text.Length / 2 + (text.Length % 2 == 0 ? 0 : 1)];
for (int i = 0; i < split.Length; i++)
{
split[i] = text.Substring(i, i + 2 > text.Length ? 1 : 2);
}
The output is "He el lo" (it doubles the second character).

Try this:
string input = "Hello"
string[] split = new string[input.Length / 2 + (input.Length % 2 == 0 ? 0 : 1)];
for (int i = 0; i < input.Length; i+=2)
{
split[i/2] = input.Substring(i, i + 2 > input.Length ? 1 : 2);
}
This steps through the input string in increments of two and takes 2 characters at a time.

I think this code will help:
string text = "1234567";
int loop = text.Length / 2 + (text.Length % 2 == 0 ? 0 : 1);
List<string> split = new List<string>();
int readTotal = 0;
int textLen = text.Length;
for (int i = 0; i < loop; i++)
{
if (textLen- readTotal >= 2)
split.Add(text.Substring(i * 2, 2));
else
split.Add(text.Substring(i * 2, 1));
readTotal += 2;
}

Related

inaccurate results with function to add an array of digits together

so i have this function:
static int[] AddArrays(int[] a, int[] b)
{
int length1 = a.Length;
int length2 = b.Length;
int carry = 0;
int max_length = Math.Max(length1, length2) + 1;
int[] minimum_arr = new int[max_length - length1].Concat(a).ToArray();
int[] maximum_arr = new int[max_length - length2].Concat(b).ToArray();
int[] new_arr = new int[max_length];
for (int i = max_length - 1; i >= 0; i--)
{
int first_digit = maximum_arr[i];
int second_digit = i - (max_length - minimum_arr.Length) >= 0 ? minimum_arr[i - (max_length - minimum_arr.Length)] : 0;
if (second_digit + first_digit + carry > 9)
{
new_arr[i] = (second_digit + first_digit + carry) % 10;
carry = 1;
}
else
{
new_arr[i] = second_digit + first_digit + carry;
carry = 0;
}
}
if (carry == 1)
{
int[] result = new int[max_length + 1];
result[0] = 1;
Array.Copy(new_arr, 0, result, 1, max_length);
return result;
}
else
{
return new_arr;
}
}
it basically takes 2 lists of digits and adds them together. the point of this is that each array of digits represent a number that is bigger then the integer limits. now this function is close to working the results get innacurate at certein places and i honestly have no idea why. for example if the function is given these inputs:
"1481298410984109284109481491284901249018490849081048914820948019" and
"3475893498573573849739857349873498739487598" (both of these are being turned into a array of integers before being sent to the function)
the expected output is:
1,481,298,410,984,109,284,112,957,384,783,474,822,868,230,706,430,922,413,560,435,617
and what i get is:
1,481,298,410,984,109,284,457,070,841,142,258,634,158,894,233,092,241,356,043,561,7
i would very much appreciate some help with this ive been trying to figure it out for hours and i cant seem to get it to work perfectly.
I suggest Reverse arrays a and b and use good old school algorithm:
static int[] AddArrays(int[] a, int[] b) {
Array.Reverse(a);
Array.Reverse(b);
int[] result = new int[Math.Max(a.Length, b.Length) + 1];
int carry = 0;
int value = 0;
for (int i = 0; i < Math.Max(a.Length, b.Length); ++i) {
value = (i < a.Length ? a[i] : 0) + (i < b.Length ? b[i] : 0) + carry;
result[i] = value % 10;
carry = value / 10;
}
if (carry > 0)
result[result.Length - 1] = carry;
else
Array.Resize(ref result, result.Length - 1);
// Let's restore a and b
Array.Reverse(a);
Array.Reverse(b);
Array.Reverse(result);
return result;
}
Demo:
string a = "1481298410984109284109481491284901249018490849081048914820948019";
string b = "3475893498573573849739857349873498739487598";
string c = string.Concat(AddArrays(
a.Select(d => d - '0').ToArray(),
b.Select(d => d - '0').ToArray()));
Console.Write(c);
Output:
1481298410984109284112957384783474822868230706430922413560435617

How to print a string in diamond format in c#

If I have a word "start", I want to print like this using for loop
a
tar
start
tar
a
How to print string in c# when it is taking string with odd number length as input from the user eg: "START", "QUESTIONS"
Here is my code
string input;
for (int i = 1; i <= input.Length; i++)
{
for (int j = 0; j < (input.Length - 2); j++)
Console.Write(" ");
for (int j = number; j < (number - 1); j--)
{
Console.Write(input[j]);
}
for (int k = number; k < i && k > 0; k++)
Console.Write(input[k]);
Console.WriteLine();
}
I doubt if this Linq routine will be accepted as a homework solution, however it could be useful for you for testing your own code:
String source = "start";
String result = String.Join(Environment.NewLine, Enumerable
.Range(0, source.Length)
.Select(index => source.Length - Math.Abs(index - source.Length / 2) * 2)
.Where(length => length > 0) // for even size words, e.g. "star"
.Select(length => source
.Substring((source.Length - length) / 2, length)
.PadLeft((source.Length - length) / 2 + length, ' ')));
// Test
// a
// tar
// start
// tar
// a
Console.Write(result);
If you really need a for-loop solution, you can do this
string input = "questions"; //for example
if (input.Length % 2 == 0)
return; //as per given condition, only ODD length strings
var isReducing = false;
for (int i = 0, len = 1, startIndex = (input.Length - 1) / 2; i < input.Length; i++)
{
var str = input.Substring(startIndex, len);
Console.WriteLine(str.PadLeft(len + startIndex, ' '));
if (len == input.Length)
isReducing = true;
startIndex = isReducing ? startIndex + 1 : startIndex - 1;
len = isReducing ? len - 2 : len + 2;
}
How about 2 x for-loop :)
string s = "0123456";
int l = s.Length;
int c = l / 2 + 1; //center
for (int i = 0; i < c; i++)
Console.WriteLine(s.Substring(c - i - 1, i * 2 + 1).PadLeft(c + i, ' '));
for (int i = c - 2; i >= 0; i--)
Console.WriteLine(s.Substring(c - i - 1, i * 2 + 1).PadLeft(c + i, ' '));

C#, one-dimension wrapping array

I have a one-dimensional array, and I need to run operations on it based on the adjacents cells of every cell.
For instance:
To run the operations on the first cell, I'll need to access the last cell and the second.
The second cell, I'll need to access the first cell and the third cell.
The last cell, I'll need to access the first cell and the one before the last cell.
My code so far is:
public static int[] firstRule(int[] numberArray)
{
for (int i = 0; i < numberArray.Length; i++)
{
if (numberArray[numberArray.Length - 1 - i] == numberArray[i] + 1
&& numberArray[i + 1] == numberArray[i] + 1)
{
numberArray[i] = numberArray[i] - 1;
}
}
return numberArray;
}
But the problem with my approach is that it would only work for the first cell, as it would take the last cell and the second cell correctly, but after that, everything would fall apart. I don't like posting without a lot of code but I have no clue how to follow this up.
I am trying to achieve the following:
Those values are numbers ranging from 0 to 3. If both the cell before and the cell after is the same number, I want to change it to x + 1
For instance: suppose I have 1 0 1 2 2. I would want to change 0 to 1. As the neighbor cells are both 0.
Just keep it simple and use variables to calculate the left and right cell indices. Inside your for loop you can do this...
var leftCell = i - 1;
if (leftCell < 0)
{
leftCell = numberArray.Length - 1; // Wrap around to the end...
}
var rightCell = i + 1;
if (rightCell > numberArray.Length - 1)
{
rightCell = 0; // Wrap back around to the beginning...
}
// Now you can update your original code to use these computed indices...
if (numberArray[leftCell] == numberArray[i] + 1
&& numberArray[rightCell] == numberArray[i] + 1)
{
numberArray[i] = numberArray[i] - 1;
}
Try this out:
var len = numberArray.Length;
for (int i = 0; i < len; i++)
{
var leftIndex = (i - 1 + len) % len;
var rightIndex = (i + 1) % len;
// do your stuff with numberArray[leftIndex] and numberArray[rightIndex]
}
% is mod operator. % len allows you to stay in range 0..len-1, so you can walk through array as if it has become 'cyclic'
From your comments.
Those values are numbers ranging from 0 to 3. If both the cell before and the cell after is the same number, I want to change it to x + 1
For instance: suppose I have 1 0 1 2 2. I would want to change 0 to 1. As the neighbor cells are both 0.
I would create a new array, populate it with the values of the existing array and then change the values of the new array according to the results of the value in the existing array.
Edit as Op is getting wrong values
I suspect you may not be copying the array correctly instead:
Existing Array array // The array you are passing in as parameter.
Declare a new empty array:
int[] newArray;
int size = array.length;
for(int i =1; i<size-1;i++){
if(array[i-1]==array[i+1])){
newArray[i]=array[i]+1;
}
else{
newArray[i]=array[i];
}
}
if(array[size-1]==array[0]){
newArray[size]= array[size]+1;
}
else{
newArray[i]=array[i];
}
if(array [size]==array[1]){
newArray[0]= array[0]+1;
}
else{
newArray[i]=array[i];
}
if there is a limit to the number and it reverts to zero after 2, then just do a simple if test for that.
int[] arr = new int[] { 1, 2, 3, 4, 5 };
var triples = arr.Select((n, i) =>
{
if (i == 0)
return Tuple.Create(arr[arr.Length - 1], arr[0], arr[1]);
else if (i == arr.Length - 1)
return Tuple.Create(arr[i - 1], arr[i], arr[0]);
else
return Tuple.Create(arr[i - 1], arr[i], arr[i + 1]);
});
foreach (var triple in triples)
{
Console.WriteLine(triple.Item1 + " " + triple.Item2 + " " + triple.Item3);
}
public static void firstRule(int[] numberArray)
{
for (int i = 0; i < numberArray.Length; i++)
{
int? prevElement = i == 0
? numberArray[numberArray.Length-1]
: numberArray[i - 1];
int? nextElement = i == numberArray.Length -1
? numberArray[0]
: numberArray[i + 1];
Console.WriteLine(
String.Format("Prev: {0}; Current: {1}; Next: {2}",
prevElement,
numberArray[i],
nextElement)
);
}
}
And then calling firstRule(new int[]{ 1, 2, 3 }); prints:
Prev: 3; Current: 1; Next: 2
Prev: 1; Current: 2; Next: 3
Prev: 2; Current: 3; Next: 1
OPTION 1
assign regardless
public static int[] firstRule(int[] numberArray)
{
int left,right;
for (int i = 0, max = numberArray.Length - 1; i <= max; i++)
{
left = (i == 0) ? max : i - 1;
right = (i == max) ? 0 : i + 1;
numberArray[i] = (numberArray[left] == numberArray[right]) ? numberArray[i] + 1 : numberArray[i]; //always peforms an assignment;
}
return numberArray;
}
OPTION 2
conditionally assign
public static int[] secondRule(int[] numberArray)
{
int left,right;
for (int i = 0, max = numberArray.Length - 1; i <= max; i++)
{
left = (i == 0) ? max : i - 1;
right = (i == max) ? 0 : i + 1;
if (numberArray[left] == numberArray[right])
{
numberArray[i]++;
}
}
return numberArray;
}
OPTION 3
left and right are only used 1 time in each iteration.. so why bother assigning them to a variable???...
public static int[] thirdRule(int[] numberArray)
{
for (int i = 0, max = numberArray.Length - 1; i <= max; i++)
{
if (numberArray[(i == 0) ? max : i - 1] == numberArray[(i == max) ? 0 : i + 1])
{
numberArray[i]++; // what happens if numberArray[i] is 3, should it become 4 or 0?
}
}
return numberArray;
}
OPTION 4 (UNSAFE)
unsafe - fixed - pointers
public static int[] fourthRule(int[] numberArray)
{
unsafe {
int* pointer, right, left;
for (int i = 0, max = numberArray.Length - 1; i <= max; i++)
{
fixed (int* p1 = &numberArray[0], p2 = &numberArray[i], p3 = &numberArray[max])
{
pointer = p2;
if (i == 0)
{
left = p3;
right = pointer;
right++;
}
else if (i == max)
{
left = pointer;
left--;
right = p1;
}
else
{
left = pointer;
left--;
right = pointer;
right++;
}
if (*right == *left) {
*pointer = *pointer + 1;
}
}
}
}
return numberArray;
}
Recently came up against this myself and found this to be a solid method.
`
int length = numberArray.Length;
for (int i = 0; i < length; ++i)
{
int iMinus = (((i - 1) % length) + length) % length;
int iPlus = (((i + 1) % length) + length) % length;
}`
Something like this should work. It determines the appropriate cells for the operation in each loop and executes the operation. You didn't state what that operation was so you need to fill in the DoYourOperation method.
public static int[] processArray(int[] numberArray)
{
for (int i= 0; i < numberArray.Length; i++)
{
int firstCell;
int secondCell;
//Check if first cell
if(i == 0)
{
firstCell = numberArray[numberArray.length-1]; //Last cell
secondCell = numberArray[i++]; //Next cell
}
//Check if last cell
else if(i == numberArray.Length - 1)
{
firstCell = numberArray[i--]; //Cell before last one
secondCell = numberArray[0]; //First cell
}
else
{
firstCell = numberArray[i--];
secondCell = numberArray[i++];
}
DoYourOperation(firstCell, secondCell);
}
}

Display 1,2,3,4,5,6,8,10,11 as 1-6,8,10-11

I have this sequence 1,2,3,4,5,6,8,10,11
Expected output is 1-6,8,10-11
This problem is about formatting the sequence in easy readable form
I tried with c# and used many if & else.
Interviewer said, there is some simple algorithm to do this.
I have no idea how to achive this very simple.
Also for 1,2,3 i shown 1-3. They said its wrong!.
Is there any design pattern(interpreter) involved in this logic?
Here is one way of doing it:
int[] numbers = { 1, 2, 3, 4, 5, 6, 8, 10, 11 };
int start, end;
for (int i = 0; i < numbers.Length; i++)
{
start = numbers[i];
while (i < numbers.Length - 1 && numbers[i] + 1 == numbers[i + 1])
i++;
end = numbers[i];
if(start == end)
Console.WriteLine(start);
else
Console.WriteLine(start + " - " + end);
}
This will display subsequent numbers that grow incrementally as range. Numbers that are not increasing linearly are not written as part of a range.
Here is another version of the first approach, it utilizes the same for loop to iterate on range:
int temp = numbers[0], start, end;
for (int i = 0; i < numbers.Length; i++)
{
start = temp;
if (i < numbers.Length - 1 )
// if subsequent numbers are incremental loop further
if (numbers[i] + 1 == numbers[i + 1])
continue;
// if they are not, number at index i + 1 is a new 'start' for the next iteration
else
temp = numbers[i + 1];
end = numbers[i];
if (start == end)
Console.WriteLine(start);
else
Console.WriteLine(start + " - " + end);
}
A simple implementation in C# could look like this:
public string Format(IEnumerable<int> input)
{
var result = string.Empty;
var previous = -1;
var start = -1;
var first = true;
foreach(var i in input)
{
if(start == -1)
start = i;
else if(previous + 1 != i)
{
result += FormatRange(start, previous, first);
first = false;
start = i;
}
previous = i;
}
if(start != -1)
result += FormatRange(start, previous, first);
return result;
}
public string FormatRange(int start, int end, bool isFirst)
{
var result = string.Empty;
if(!isFirst)
result += ", ";
if(start == end)
result += start;
else
result += string.Format("{0}-{1}", start, end);
return result;
}
This will also output 1-3 for the input 1,2,3, which is perfectly valid. Without a specification what the output should be instead it's impossible to answer that part.
Probably not a suitable answer for an interview question, but using LINQ is another way to solve this.
int[] numbers = { 1, 2, 3, 4, 5, 6, 8, 10, 11 };
var remains = numbers.AsEnumerable();
while (remains.Any())
{
int first = remains.First();
int last = remains.TakeWhile((x, i) => x - first == i).Last();
remains = remains.Skip(last - first + 1);
Console.Write(first + (first == last ? "" : "-" + last) + (remains.Any() ? "," : Environment.NewLine));
}
The following groups consecutive integers, and outputs a string for each group. However, it also allows you to specify the minimum length of group which you want to hyphenate; anything less will just give you the individual numbers. Thus if you only want to hyphenate groups of 4 or more, you can pass in 4; if you want to hyphenate pairs, you can pass in 2. (I'd want to use 3 myself, but I can't tell what they want.)
It also doesn't keep any collections of numbers as it goes along, because you don't need to.
Method:
static IEnumerable<string> Group(IEnumerable<int> input, int minLength)
{
int currentStart = int.MinValue;
int currentLength = 0;
foreach (int c in input)
{
if (currentLength > 0)
if (currentStart + currentLength == c)
currentLength++;
else
{
if (currentLength >= minLength)
yield return string.Format("{0}-{1}",
currentStart, currentStart + currentLength - 1);
else
for (int i = currentStart; i < currentStart + currentLength; i++)
yield return i.ToString();
currentStart = c;
currentLength = 1;
}
else
{
currentStart = c;
currentLength = 1;
}
}
if (currentLength >= minLength)
yield return string.Format("{0}-{1}",
currentStart, currentStart + currentLength + 1);
else
for (int i = currentStart; i < currentStart + currentLength; i++)
yield return i.ToString();
}
Usage:
int minCount = 3;
int[] input = new[] { 1, 2, 3, 4, 5, 6, 8, 10, 11 };
Console.WriteLine(String.Join(",", Group(input, minCount)));
Java code:
int[] arr = {1,2,3,4,5,6,8,10,11};
int start = arr[0], last = arr[0];
String output = "";
for (int i = 1; i <= arr.length; i++)
{
if (i == arr.length || arr[i] != last+1)
{
if (output.length() != 0)
output += ",";
if (start == last)
output += start;
else
output += start + "-" + last;
if (i != arr.length)
start = last = arr[i];
}
else
last = arr[i];
}
System.out.println(output);
Heres my best attempt. Not clever, but simple enough to satisfy that requirement I believe. I'm still pretty confused as to why "1-3" was wrong though....
var numbers = new int[] { 1, 2, 3, 4, 5, 6, 8, 10, 11, 12 };
var groups = new Dictionary<int, int>();
groups.Add(numbers.First(), numbers.First());
foreach (var num in numbers.Skip(1))
{
var grp = groups.Last();
if (grp.Value + 1 == num)
{
groups[grp.Key] = num;
}
else
{
groups.Add(num, num);
}
}
var output = string.Join(",", groups.Select(grp => (grp.Key == grp.Value) ? grp.Value.ToString() : grp.Key.ToString() + "-" + grp.Value.ToString()));
Note: of course using the dictionary and linq etc is completely unnecessary (and way too specific for an answer requiring an algorithm), but I thought it highlighted the grouping aspect of the problem nicely
This is no valid C# code but to show the Idea.
Sort the list from Min to Max then do this:
For i = Min to Max
{
if i < MaxFound
continue;
int step = 1;
Output = i;
while Found(i + Step)
{
Step++;
MaxFound = i + Step;
}
if i < MaxFound
Output = (i + "-" + MaxFound);
Output += ", ";
}
Here is one of the approach:
public static void main(String[] args) {
print(1, 2, 3, 4, 5, 7, 9, 10, 12);
}
public static void print(int ... nums) {
System.out.print(nums[0]);
int idx = 1;
for(int i = 1; i < nums.length; i++, idx++) {
if(nums[i] - nums[i - 1] != 1) {
if(idx > 1) {
System.out.print(" - " + nums[i - 1]);
}
System.out.print(", " + nums[i]);
idx = 0;
}
}
if(idx > 1)
System.out.println(" - " + nums[nums.length - 1]);
}
Here's a Haskell version:
import Data.List
parseRange [] = ""
parseRange n =
let range = takeWhile (\x -> isInfixOf [x,x+1] n) n
in if not (null range)
then show (head range) ++ "-" ++ show (last range + 1)
++ (if length (tail n) > 1 then "," else "")
++ parseRange (drop (length range + 1) n)
else show (head n) ++ (if null (tail n) then "" else ",")
++ parseRange (drop 1 n)
Output:
*Main> parseRange [1,2,3,4,5,6,8,10,11]
"1-6,8,10-11"
And a way to do it with fold in F# - just for fun.
let parseRange numbers =
numbers
|> Seq.fold
(fun list n ->
match list with
|(a,b) :: tail when b+1 = n -> (a, n) :: tail
|_ -> (n,n) :: list) []
|> List.rev
|> Seq.map (fun (a,b) -> if a = b then sprintf "%i" a else sprintf "%i-%i" a b)
|> String.concat ","

Creating 2 numbers from a one #C

I should create two new numbers from a one, the first group will contain digits which are divisible by 2 and the other group will contain the others.
int checkCount = 94321, num1 = 94321, count2 = 0, countRest = 0;
while (checkCount > 0)
{
if (checkCount % 2 == 0)
count2++;
else
countRest++;
checkCount /= 10;
}
int[] a = new int[count2];
int[] b = new int[countRest];
int k2 = 0, kRest = 0;
for (int j = 0; j < a.Length + b.Length; j++)
{
if (num1 % 2 == 0)
{
a[k2] = num1 % 10;
k2++;
}
else
{
b[kRest] = num1 % 10;
kRest++;
}
num1 /= 10;
}
I created two arrays with the numbers I should use, now how can I build two INT varabile when each one contains all of the numbers together from the array?
Example:
If I have this number - 12345 so
var = 24, other var = 135
If you have another solution without arrays I think it will be better.
Thank you.
Why not just:
int decimalMaskA = 1;
int decimalMaskB = 1;
while (checkCount > 0)
{
if (checkCount % 2 == 0)
{
count2 = count2 + (checkCount % 10)*decimalMaskA;
decimalMaskA *= 10;
}
else
{
countRest = countRest + (checkCount % 10)*decimalMaskB;
decimalMaskB *= 10;
}
checkCount /= 10;
}
count2 and countRest will contain those numbers (135 and 24) instead of counts.
This splits number 12345 to numbers 135 and 24.
int checkCount = 12345;
int even = 0;
int odd = 0;
int reverseEven = 0;
int reverseOdd = 0;
while (checkCount > 0) {
int current = checkCount % 10;
if (current % 2 == 0) {
reverseEven = 10 * reverseEven + current;
} else {
reverseOdd = 10 * reverseOdd + current;
}
checkCount /= 10;
}
while (reverseEven > 0) {
even = 10 * even + reverseEven % 10;
reverseEven /= 10;
}
while (reverseOdd > 0) {
odd = 10 * odd + reverseOdd % 10;
reverseOdd /= 10;
}
Console.WriteLine("even: {0}", even);
Console.WriteLine("odd: {0}", odd);
If I understand what you're looking for this will do the trick:
//Setup a sample array
int[] a = new int[2];
a[0] = 2;
a[1] = 4;
//Convert each item to a string, convert that to a string array, join the strings and turn into an int
int output = int.Parse(String.Join("", a.Select(s => s.ToString()).ToArray()));
This works for me:
int number = 12345;
string result1 = "";
string result2 = "";
string numberString = number.ToString();
for (int i = 0; i < numberString.Length; i++ )
{
if (numberString[i] % 2 == 0)
{
result1 = result1 + numberString[i];
}
else
{
result2 = result2 + numberString[i];
}
}
int evenNumbers = int.Parse(result1);
int oddNumbers = int.Parse(result2);
How can I build two INT varabile
when each one contains all of the
numbers together from the array?
I can't say for sure, but I think you're asking how to assemble a number given each of its digits in decreasing order of significance.
To 'append' a digit to a number, you can multiply the number by 10 and then add the digit to that. To create the 'assembled' number, you can perform this operation for each digit in the array,
int[] digits = ...
int num = digits.Aggregate(0, (numSoFar, digit) => 10 * numSoFar + digit);
As a loop, this would look like:
int num = 0;
foreach(int digit in digits)
{
num = 10 * num + digit;
}
Try this with LINQ,
int num = 92345;
string strNum = Convert.ToString(num);
var divisibleby2 = from c in strNum
where int.Parse(c.ToString()) % 2 == 0
select c.ToString();
var notDivisibleby2 = from c in strNum
where int.Parse(c.ToString()) % 2 != 0
select c.ToString();
int int_divisibleby2num = int.Parse(String.Join("", divisibleby2.ToArray()));
int int_Notdivisibleby2num = int.Parse(String.Join("", notDivisibleby2.ToArray()));
Every programmer should write wacky code at least once a day:
int checkCount = 12345, numEven, numOdd;
Boolean result;
result = int.TryParse(checkCount.ToString().Replace("0", "").Replace("2", "").Replace("4", "").Replace("6", "").Replace("8", ""), out numOdd);
result = int.TryParse(checkCount.ToString().Replace("1", "").Replace("3", "").Replace("5", "").Replace("7", "").Replace("9", ""), out numEven);
Another solution could be...
var num1 = 94321;
var oddFinal = 0;
var evenFinal = 0;
var odd = new List<int>();
var even = new List<int>();
while( num1>0 )
{
if( num1 % 2 == 0 )
odd.Add( num1 % 10 );
else
even.Add( num1 % 10 );
num1 = num1 / 10;
}
for (int i = 0; i < odd.Count; i++)
{
oddFinal += odd[i] * (int) Math.Pow(10,i);
}
for (int i = 0; i < even.Count; i++)
{
evenFinal += even[i] * (int) Math.Pow(10,i);
}

Categories