Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm using below code to find out what is the length of a given string:
string foo = "Welcome to stack overflow";
int strlen;
for (int i=0; i<foo.length; strlen++){}
Console.WriteLine(strlen.ToString());
But the code never leaves the loop.
Well, that's weird.
I don't understand your logic, you already have the string length, why use a loop to apply it to another int?
But, well, who am I to judge?
The problem on your loop is that you're not increasing the value of i.
Do this instead:
for (int i=0; i<foo.Length; i++)
{
strlen++;
}
You could remove the loop and do this to your code:
string foo = "Welcome to stack overflow";
Console.WriteLine("String length: " + foo.Length.ToString());
Edit:
As mentioned in the comments:
the length property must have it's first letter uppercased, since C# is case sensitive. - Jon Skeet
You are never increasing "i" so the "i < foo.length" will always be true
You should iterate over i, not over strlen:
for (int i=0; i<foo.length; i++){}
You have one typo (foo.Length, not foo.length) and two errors:
Do not forget to assign 0 on local variabale declaration (int strlen = 0)
Do not forget to increment counter (i++)
Something like that:
string foo = "Welcome to stack overflow";
// error: assign 0 to strlen
int strlen = 0;
// Typo: foo.Length instead of foo.length
// error: do not forget to increment "i" as well as "strlen"
for (int i = 0; i < foo.Length; strlen++, i++) {}
// 25
Console.WriteLine(strlen.ToString());
Test:
// 25
Console.WriteLine(foo.Length);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm a begginer in c# and I'm trying to solve problems and exercises, what I have to do is write a method that compares the jump height of a jumper to the height of the hurdles, if the jump height is equal to or greater than the biggest hurdle, the method should return true. The heights of the hurdles are imputed in the form of an array, and the jump height is an integer.
The code I wrote was:
public static bool hurdleJump(int[] hurdles, int jumpHeight)
{
int height= 0;
for(int i = 0; i == hurdles.Length; i++)
{
if(hurdles[i] > height)
height = hurdles[i];
}
if(jumpHeight >= height)
{
return true;
}
else
{
return false;
}
}
I used a for loop to find the biggest value inside of the array and then compared that to the int, but this code doesn't work with some arrays and I have no idea why, what am I missing?
Your loop is not running because of the condition;
for(int i = 0; i == hurdles.Length; i++)
You must use:
for(int i = 0; i < hurdles.Length; i++)
This should do it, much simple than using a loop:
public static bool hurdleJump(int[] hurdles, int jumpHeight)
{
return jumpHeight >= hurdles.Max();
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am calculating month's days based on cmbYear & cmbMonth with the below code:
int days = DateTime.DaysInMonth(Convert.ToInt16(cmbYear.SelectedItem), Convert.ToDateTime("01-" + cmbMonth.SelectedItem + "-2011").Month);
What I am trying to achieve, using for loop, is to create a drop down list that includes all numbers starting from 1 to int days for cmbDay combo box. Below is my code which generates an error when I am trying to import the end point of the code i = days;. Error in Cannot implicitly convert type int to bool.
Any help will be appreciated.
for (int i = 1; i = days; i++)
{
}
the Loop should Looks like
for (int i=1; i <= days; i++)
or
for (int i=1; i < days; i++)
depends on what you Need.
You have to use <= instead of =
for (int i=1; i <= days; i++)
{
}
The second parameter is the condition. As long as it is true, the loop goes on
for (C# reference)
The for statement defines initializer, condition, and iterator
sections:
for (initializer; condition; iterator)
body
The condition section
The condition section, if present, must be a boolean expression. That
expression is evaluated before every loop iteration. If the condition
section is not present or the boolean expression evaluates to true,
the next loop iteration is executed; otherwise, the loop is exited.
You had i = days which is an assignment, and results in an int which is not a condition (bool).. What you probably intended was i == days which is still wrong.
What you needed was the following, which says "while i is less then days, loop the body"
for (int i=1; i < days; i++)
it's a simple syntactical error. you've misses an '<'
for(int i=1; i<=days;i++){..}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
For some reason a value 0 is changed to 48 from 0. I get a string of "100" but it should be "001" so I used this code to change the order and place it in an array so I can get each value:
var inputArray = binarystring.ToCharArray().Reverse();
InputValues = inputArray as char[] ?? inputArray.ToArray();
But when I check a value, say InputValues1, as a value I get 48. I just need the "0" or "1". Thank you.
Using the suggestion I still get the issue
As mentioned by #Akash KC, you convert it to char array, where 48 is the char '0', use below function instead:
public static string ReverseStringDirect(string s)
{
char[] array = new char[s.Length];
int forward = 0;
for (int i = s.Length - 1; i >= 0; i--)
{
array[forward++] = s[i];
}
return new string(array);
}
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
string text1 = "abcde";
string text2 = "fgchi";
I want to check if the 2 strings have the same characters at the same index and if they do then let the place where they are the same be printed.
for (int i = 0; i < text1.Length; i++)
if (text1[i] == text2[i])
Console.WriteLine("Character {0} at index {1}", text1[i], i);
Considering your strings have the same length.
Edit: if I should not give answers to trivial tasks such as this and instead encourage the user to find it by himself, then please point out that to me. I'm new around. [I suppose it's obvious, so i'm just not gonna do it, and adjust]
Maybe something like the following code helps. And it shouldn´t be important how long each string is with that. Maybe the string.Format isn´t needed.
private string charMatch(string str_a, string str_b)
{
int char_a = str_a.Count();
int char_b = str_b.Count();
int runs = 0;
StringBuilder sb = new StringBuilder();
if (char_a <= char_b) { runs = char_a; }
else { runs = char_b; }
for (int i = 0; i < runs; i++)
{
if (str_a[i] == str_b[i])
{
sb.Append(string.Format("Match found at {0} \n", i));
}
}
return sb.ToString();
}
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);