Reputation: 51
how can i check if number is increasing? 123456-true 423659-false
i try
while (newNumber != 0)
{
string a = newNumber.ToString().Substring(0, 1);
string b = newNumber.ToString().Substring(0, 2);
input1 = Convert.ToInt32(a);
input2 = Convert.ToInt32(b);
if (input1 < input2)
{
////how do i increase substring a&b +1 ?
}
}
}
also, how can i do it without the substring method?
Upvotes: 0
Views: 5043
Reputation: 650
The method you're using isn't a very good one. First of all, b will always be larger than a, because you're feeding 1 and 2 into the length function, not the position. So, for example, if you take the number 12345, a will be 1, and b will be 12. Also, the Substring method is calculated when it's called, so you can't modify the parameters after defining it without calling Substring again. Think of it like this: if you assign the result of a method to a variable, then the method has to have calculated that result, and so if you want a different result, it will have to calculate it again. However, you don't need to use the Substring method at all. In C#, strings can be accessed by index, so you can say myString[0]
for the first element (remember, indices in C# are 0 indexed, so the first item is index 0). To do what you're checking, you can use a for loop, like so:
if (newNumber != 0)
{
string a = newNumber.ToString();
for(int i = 1; i < a.Length; i++)
{
int input1 = Convert.ToInt32(a[i-1]);
int input2 = Convert.ToInt32(a[i]);
if (input1 >= input2) //Note the reversed condition
{
return false; //Gives the false result
}
}
return true; //Computation finished, so the number is increasing
}
}
This method should be quick, and do pretty much exactly what you want, if I've understood your question correctly. Also, I'm not sure why you were using a while loop, since you don't change "newNumber" in the body of the loop, the program will compute in a loop until something stops it. You should use an if statement. While loops will loop through the body over and over, testing the condition before it starts computation each time, while an if statement only runs once. In general, use if statements where you can, because while loops have the potential to run indefinitely (since you can get into a state where the condition is never met, and it will just loop over and over forever).
Upvotes: 1
Reputation: 35890
There's no need to convert the number back and forth between string
and int
. In C# strings are enumerable as character arrays, and characters are comparable between each other:
bool IsIncreasing(int number)
{
string text = number.ToString();
char previous = '0';
foreach(char c in text)
{
if(c <= previous)
return false;
previous = c;
}
return true;
}
Upvotes: 2
Reputation: 8459
You can use this LINQ chain. asc1
and asc2
are boolean values, the first will be true, the second will be false. What the chain does is to check if the amount of characters with index equals to zero or with index greater than zero but still greater than the previous is equals to the string's length. It doesn't parse the characters because, assuming that the strings are only numbers, their ASCII values will fall in the range 30..39, thus being still valid in the check.
var s1 = "123456";
var s2 = "423659";
var asc1 = s1.Where((c, i) => i == 0 || c > s1[i - 1]).Count() == s1.Length;
var asc2 = s2.Where((c, i) => i == 0 || c > s2[i - 1]).Count() == s2.Length;
Upvotes: 0
Reputation: 590
you can just see if the element with index i in the string is bigger than the element i-1 (for each element with index staring in 1).
Upvotes: 0