How to Zip two numbers using c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
string A = "1234"
string B = "567890"
I want to zip the numbers. Out put should display as "1526374890"
what is the best way to achieve this using C# code.

You can do that with the following code. It does not make any assumptions about which string is longer.
string A = "1234";
string B = "567890";
char[] chars = new char[A.Length + B.Length];
int charsIndex = 0;
for (int i = 0; i < A.Length || i < B.Length; i++)
{
if(i < A.Length)
chars[charsIndex++] = A[i];
if(i < B.Length)
chars[charsIndex++] = B[i];
}
string result = new string(chars);
Console.WriteLine(result);

Related

How to remove count of specific byte in array C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I want to remove bytes from an array, I don't want to remove all bytes 0x6f I just want to remove two only of them. This is my code:
string msg = "gooooooal";
byte[] oldArray = Encoding.GetEncoding(1256).GetBytes(msg);
byte[] newArray = oldArray.Where(b => b != 0x6f).ToArray();
You can first find the position and then remove them
byte[] oldArray = Encoding.GetEncoding(1256).GetBytes(msg);
int howManyToRemove = 2; //How many items to remove
var positions = new List<int>();
int lastPos = -1;
for (int i = 0; i < howManyToRemove; i++)
{
var position = Array.IndexOf(oldArray, (byte)0x6f,lastPos+1);
if (position == -1)
{
break;
}
lastPos=position;
positions.Add(position);
}
byte[] newArray = oldArray.Where((val, idx) => !positions.Contains(idx)).ToArray();
If I understood your problem then if want to delete two occurrences of the letter o from your string because the 0x6f ASCII value is 111 which is the letter o.
and for that, you are making the solution very complex.
if can simply do like this.
string s = "gooooooal";
string output = removeChar(s, 'o'); //output will be gooool
static string removeChar(string s,
char ch)
{
int count = 2;
for (int i = 0; i < s.Length; i++)
{
// If ch is found
if (s[i] == ch && count > 0)
{
s = s.Substring(0, i) +
s.Substring(i + 1);
count--;
}
}
return s;
}

Generate random sequence of AlphaNumeric [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want a code to generate sequence of Alphanumeric character like LLNNLLNNLL where L is the Letter and N is the number. For example if the length is 5 the sequence will be LLNNL and if the length is 6 the sequence will be LLNNLL and if 7 it would be LLNNLLN.
string alphabets = "ABCDEFGHIJKLMNPQRSTUVWX";
int length = model.VALUE_INT;
string result = "";
for (int i = 0; i < length; i++)
{
int next = _random.Next(23);
result += alphabets.ElementAt(next);
result += _randomNum.Next(1, 9);
}
This is what I have tried but condition is not fulfilling
For each position (value of i) of your output string you need to check if you need a character or an integer.
Since the pattern repeats every 4 positions, you can achieve this using the modulo operator:
for (int i = 0; i < length; i++)
{
switch (i % 4)
{
case 0:
case 1:
int next = _random.Next(23);
result += alphabets.ElementAt(next);
break;
case 2:
case 3:
result += _randomNum.Next(1, 9);
break;
}
}
Or another possibility: Add blocks of LLNN and then truncate the result to the needed length...
for (int i = 0; i <= (length/4); i++)
{
result += alphabets.ElementAt(_random.Next(23));
result += alphabets.ElementAt(_random.Next(23));
result += _randomNum.Next(1, 9);
result += _randomNum.Next(1, 9);
}
result = result.Substring(0,length);
If you want to improve these lines, you can use a StringBuilder

C# how to repeat an action [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Basically trying to shorten this:
octree[nodeIndices[0]].nodes[nodeIndices[1]]
.nodes[nodeIndices[2]].nodes[nodeIndices[3]].nodes[nodeIndices[4]]
I thought something like this could work, but not sure exactly how:
octree[nodeIndices[0]].(Enumerable.Repeat(node[nodeIndices[i]], 4)
edit: here's more code
OctreeNode[] nodes = octree;
ushort subdivisions = 1;
while (true)
{
ushort nodeIndex = 0;
OctreeNode node = nodes[nodeIndex];
if (node == null)
{
node = new OctreeNode();
node.nodes = new OctreeNode[8];
nodes = node.nodes;
}
subdivisions++;
if (subdivisions > 4)
break;
}
// the actual octree variable is unchanged
var result = octree[nodeIndices[0]];
for (int i = 1; i <= 4; i++)
result = result.node[nodeIndices[i]];
// do something with result;

Having trouble counting in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having trouble with this. How can I count a length I want input to.
Lets say I ask a user an input. He enters 10. It counts 1,2,3,4,etc... 10.
Or if users enter 5, 1,2,3,4,5 is output
Thanks.
EDIT:
I am sorry. This isn't homework. School doesn't start till next week and I am practicing.
Sorry, I should have given code.
This is what I had that does work
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i < length; i++)
{
Console.WriteLine(i);
}
I am just assuming since I am new that I did some sloppy code and am looking for maybe something cleaner. Or another point of view for it.
update your code with <=
Console.WriteLine("Enter Length");
int length = int.Parse(Console.ReadLine());
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
You just need to change the '<' operator to '<=':
for (int i = 0; i <= length; i++)
{
Console.WriteLine(i);
}
string length;
Console.Write("Enter Length: ");
length= Console.ReadLine();
for (int i = 1; i <= Int32.Parse(length); i++)
{
Console.WriteLine(i);
}
Console.ReadKey();

Converting a String to Collapse Multiple Instances of Letters into a Letter and Numeral Representation of it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example if I were to have
Textbox1.Text = "aaaabbbccDdff";
How would I convert that to a letter and numeral representation of the letters based on how many of them are in a row, ie having it turn into
Textbox2.Text = "a4b3c2Ddf2";
Try with this:
string s = Textbox1.Text; //"aaaabbbccfffff";
string r = "";
int count = 0;
char currChar = s[0];
for(int i = 0; i < s.Length + 1; i++)
{
if(i >= s.Length || currChar != s[i])
{
r += currChar + count.ToString();
count = 1;
if(i < s.Length)
currChar = s[i];
}
else count++;
}
Textbox2.Text = r;
For the Linq fanatics:
var r = string.Join("", s.GroupBy(c=>c).Select(x=>x.Key+x.Count().ToString()));

Categories