Creating a string using StringBuilder in 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
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.

Related

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());
}

Convert Instagram Id to Url Segment [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 5 years ago.
Improve this question
Currently, I am trying to convert coffee script from there https://github.com/slang800/instagram-id-to-url-segment to C# but I am unable to convert because I have less exp in coffee script. What I need to do is simple but I am unable to guess the algo. If id is 1038059720608660215 then value should be 5n7dDmhTr3. The complete code of algo is available at github.
Try this
static void Main(string[] args)
{
string[] convert = {
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z","0","1","2","3","4","5","6","7",
"8","9","-","_"
};
Int64 input = 1038059720608660215;
string output = "";
for (int i = 9; i >= 0; i--)
{
long digit = (input >> (6 * i)) & 0x3F;
output += convert[digit];
}
Console.WriteLine(output);
Console.ReadLine();
}

Can we add two integer variable names in C# rather than values? [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 5 years ago.
Improve this question
In c # i'm trying to add two different integer name, is it possible
int variablename;
for(int i=0;i<10;i++)
{
Objectname.variablename+i
}
No, you cant. C# is strongly typed programming language. What you can do, is to use Dictionary for example, like this:
Dictionary<string, int> names = new Dictionary<string,int>();
for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}
var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
If I understand you correctly you want to get the name of your variable and then change the name for each instance based on the iteration, thus you want to add a numeric value to the name. You can achieve this as follows:
string testVariable = "";
int i = 1;
// Get the name of your variable. This will result in "testVariable" as value.
string nameOfTestVariable = nameof(testVariable);
// Now add your integer to the name, but add it as a string.
nameOfTestVariable += i.ToString();
//Result, value of nameOfTestVariable: "testVariable1"
To get the datatype of your variable instead:
int a = 0;
int b = 1;
string dataTypesOfVariables = a.GetType().Name;
dataTypesOfVariables += b.GetType().Name;
//Result, value of dataTypesOfVariables: "Int32Int32"

String vector with user input [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Stuck for hours on this task. I really appreciate help, it is not shown in the literature how this is to be done. I can create the vector but after that I'm totally blocked.
Create a string vector with five elements. The user will then enter 5 names via a for loop. The program then writes these names via another for loop.
Please type out the code so I can understand. Without solving this first task I can't do the other ones.
Here's my futile attempt so far
int [] namn = new int [5];
for (int i = 0; i < 5; i++);
{
Console.Write("Ange fem namn");
string str = Console.ReadLine();
int names = Convert.ToInt32(str);
}
It seems that the main (and the most difficult error to find) is ; after for loop
for (int i = 0; i < 5; i++);
this loop does nothing five times.
//DONE: "string[]" - Create a STRING vector with five elements...
string [] namn = new string [5];
//DONE: namn.Length - no magic numbers (5)
for (int i = 0; i < namn.Length; i++) // !!! no ";" !!!
{
Console.Write("Ange fem namn");
//DONE: put into array, not to a local variable
namn[i] = Console.ReadLine();
}
// Print out the names
foreach(var item in namn)
Console.WriteLine(item);

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