char array to int array and delete an char 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 9 years ago.
Improve this question
im trying to make a song in beeps with pi and i need to delet the dot (pi1[1]) () and i need to convert the char array pi1 to int array pi2 (*)
double a = Math.PI;
string b=a.ToString();
char[] pi1 = b.ToCharArray();
* pi1[1] = '0';
int[] pi2;
for (int i = 0; i < pi1.Length; i++)
{
** pi2[i] = int.Parse(pi1[i].ToString());
}
//for (int i = 0; i > 40; i++)
//{
// Console.Beep(100*c, 100);
//}

The part that seems to be causing the problem is the need to not try to convert the decimal point. You can do this using string.Replace.
The following is a one-line solution to get the first 15 digits of pi as an array of integers:
int[] piDigits = Math.PI.ToString()
.Replace(".", "")
.Select(c => c - '0')
.ToArray();

Assuming that pi1 consists entirely of the characters 0–9, you could parse it into an int array using:
int[] pi2 = pi1.Select(c => c - '0').ToArray();

Related

Make the largest number of the number [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 2 years ago.
Improve this question
i try to make the largest number, of entered number. Have some ideas, but all of them end by error.
int n = 123 // in the end shell be 321, 567 -> 765, 184 -> 841
StringBuilder str = new StringBuilder(n.ToString());
int[] aray = new int[3];
for (int i = 0;i < str.Length;i++) {
aray[i] = int.Parse(str[i].ToString());
}
aray.OrderBy(x => x);
var a = aray.Join(' '); //eror
Console.WriteLine(a);
Well, we have two cases: postive and negative numbers:
n = 43125 -> 54321
n = -43125 -> -12345
We can hold both cases with a help of Linq:
using System.Linq:
...
string result = string.Concat(n < 0
? n.ToString().OrderBy(c => c)
: n.ToString().OrderByDescending(c => c));
Here we exploit the fact that '-' < '0' and we don't have to do any special work for - in case of negative numbers. If you want to obtain int value, just add int.Parse:
int result = int.Parse(string.Concat(n < 0
? n.ToString().OrderBy(c => c)
: n.ToString().OrderByDescending(c => c)));
but be careful: large n will cause integer overflow:
n = 1234567890 -> 9876543210 > int.MaxValue
You have a lot of code to accomplish something very simple:
var ans = int.Parse(String.Concat(n.ToString().OrderByDescending(dc => dc)));

Splitting numbers into list/array (e.g. 1998 to 1,9,9,8) [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 2 years ago.
Improve this question
this is my first post so excuse me if I am not being clear enough.
As the title says I want to split a number into smaller parts, for example 1998 to 1,9,9,8. I have looked around but I am stuck trying to figure out the right term to search for.
Reason why I want this done is to later multiply every other number with either 1 or 2 to be able to examine whether it's correct or not.
Sorry I can't provide any code because I am not sure how I should tackle this problem.
As #mjwills said, mod is what you want.
static IEnumerable<int> Digitize(int num)
{
while (num > 0)
{
int digit = num % 10;
yield return digit;
num = num / 10;
}
}
And then call it like so:
static void Main(string[] _)
{
int num = 1998;
int[] digits = Digitize(num).ToArray();
Console.WriteLine($"Original: {num}");
foreach (var digit in digits)
Console.WriteLine(digit);
}
make a string of that number and make it like this
```
int a = 1990;
int[] intArray = new int[a.ToString().Length];
int index = 0;
foreach( char ch in a.ToString())
{
intArray[index] = int.Parse(ch.ToString());
index++;
}

Master mind in console [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 5 years ago.
Improve this question
I want to make some sort of mastermind game in the console where the computer generates a 3 digit code and you have to guess it. The computers also tells you which digit you get right after you take a guess. How would i best do this? I don't know a way to "chop up" input so that i can make the computer check it against the digit it has.
example:
computer generates random code: 123
you guess: 321
computer : -*- (the star indicates which digit you got right)
I have an idea about how i'd indicate what digits are right or not, but i don't know how to separate the input into parts
Convert the computer generated random number into a string. The user input is given as string anyway.
The string type has an indexer allowing you to access single characters
string s = "abc";
char a = s[0];
char b = s[1];
char c = s[2];
for (int i = 0; i < s.Length; i++) {
Console.WriteLine($"s[{i}] = '{s[i]}'");
}
You write char literals as
char ch = 'x';
using single quotes.
Example:
string userInput = Console.ReadLine();
int code = SomehowGenerateRandomCode();
string codeString = code.ToString();
// Let's assume that both numbers have 3 digits.
for (int i = 0; i < 3; i++) {
if(userInput[i] == codeString[i]) {
// digits are equal
} else {
// digits are different
}
}
To split an int to its digits you can convert to string an split like:
int i = 123;
string myInt = i.ToString();
List<int> digits =new List<int>();
foreach (char c in myInt)
{
digits.Add(int.Parse(c.ToString());
}

Creating a string using StringBuilder 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 6 years ago.
Improve this question
I am in the process of converting a VB application. I am trying to create a string using StringBuilder as I cannot change a string using the Mid function. I have a loop although it only adds the first character to the string the rest is just white space. How can i add all the data to the string?
The code snippet is below; thanks for your help
int table = 0;
string tableData = Strings.StrDup(253, " ");
int i = 0;
string listData = null;
int pointer =0;
StringBuilder sb = new StringBuilder(tableData);
sb.Insert(0, Strings.StrDup(253, Strings.Chr(32)));
sb.Insert(0, typeOfTable + Strings.Chr(0));
pointer = 2;
for (i = 0; i <= lstTABLE.Items.Count -1; i++)
{
listData = lstTABLE.Items[i].ToString();
table = Convert.ToInt32(-(Conversion.Val(listData) * 10));
sb.Insert(pointer, Functions.FNCodeTwoChar(table));
pointer +=2;
}
sb.Insert (202,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblStartTable.Text))));
sb.Insert(205,Functions.EncodeKP(Convert.ToSingle(Conversion.Val(lblEndTable.Text))));
sb.Insert(208, Strings.Space(36));
sb.Insert(244, " 0");
sb.Insert(248, " 0");
The answer to the question of "I have a loop although it only adds the first character to the string the rest is just white space. How can i add all the data to the string?" is:
The second character you add to the string is Strings.Chr(0) which is the string terminator character. When C# (or VB.Net for that matter) hit this character it stops reading the string.

Autonumber with alternating number and letters [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 8 years ago.
Improve this question
I want an auto-numbering class in c# which would generate numbers of 8 digit length in the following format i.e. 1A2B3C4D..one number followed by one letter.Any suggestions??
Pseudocode for generating such string:
String result = "";
for ( int i = 0; i < 8 ; i++)
{
if ( i % 2 == 0)
{
// random(a,b) returns random value between or equal to a-b
result.append(random(0,9).toString());
}
else
{
result.append(random(65,90).toChar()); // Generating a random value between 65-90 (A-Z in ascii)
}
}
Edit:
Or as Sayse suggested:
String result = "";
for (int i = 0; i< 4; i++)
{
result.append(random(0,9).toString());
result.append(random(65-90).toChar());
}

Categories