How would I increase the value of this array +1?
I have some code but its not getting +1 the value of that array.
By +1 I mean the array value 1 above that one, so if my array contains the words: Hi, Hey Hello, instead of it displaying Hi I want it so display Hey.
Heres the code I have but its not moving it +1 up:
string[] u = getBetweenAll(vid, "</id><published>", "</published><updated>");
for (int i = 0; i < listView1.Items.Count; i++)
{
string input = u[i] + 1;
int index = input.IndexOf("T");
if (index > 0)
input = input.Substring(0, index);
listView1.Items[i].SubItems.Add(input);
}
Did you mean:
string input = u[i + 1];
string input = u[i + 1];
you need to increment the indexer
Related
Im new to programming i use c# to solve some problem but i have problem when user input two integer no in same-line and i want to look for zero and remove it from each no i think by replacing with the next array cell i should convert integer to string again but i cant do it please help me
string[] arr = Console.ReadLine().Split(' ');
string a = arr[0];
string b = arr[1];
int[] m = new int[1];
int[] n = new int[1];
for (int i = 0; i < a.Length; i++)
{
if (a[i] == 0 && a[a.Length - 1] != 0)
{
for (int j = i; j < a.Length; j++)
{
//****m[i] = int.Parse(a[i]); the error here cant convert from char to string ??
// a[i] = a[i + 1];**\\ Error CS0200 Property or indexer 'string.this[int]' cannot be assigned to -- it is read only**
I don't really know what you're trying to do, but if you're trying to convert the char into an ASCII value, then you would have to use the static method of Char called GetNumericValue.
Information on GetNumericValue can be found here: https://learn.microsoft.com/en-us/dotnet/api/system.char.getnumericvalue?view=netcore-3.1
My goal was to get a 3DES password and for that im missing the last 6 digits.
for (int i = 0; i <= 16777215; i++)
{
string hexValue = i.ToString("X").PadLeft(6, '0');
}
You could just get the maximum number in your set of hexadecimals which goes from [0,FFFFFF] in decimal the set goes from [0, 16777215]
for(int i = 0; i <= 16777215; i++) {
string hexValue = i.ToString("X");
// your logic goes here...
}
This should do it:
var allHexadecimals = Enumerable.Range(0, 0xFFFFFF + 1)
.Select(i => i.ToString("X").PadLeft(6, '0'));
You can actually convert directly to Hex from c#, check out this article:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types
e.g. for(int i = 0x0; i < 0xff;i+=0x01) {Console.WriteLine(iConsole.WriteLine(value.ToString("X"));}
I am trying to insert an int value in int array at the user input index and getting an error: Index was outside the bounds of the array. The error is in the while loop, but the solution does not use any methods like Array.Add etc.
Console.WriteLine("Enter the length of Linear Array");
int length = Convert.ToInt32(Console.ReadLine());
int[] LinearArr = new int[length];
Console.WriteLine("Maximum number of input : {0}",length);
for (int i = 0; i < LinearArr.Length; i++)
{
LinearArr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("What number you want to insert and at what index");
int InsertNum = Convert.ToInt32(Console.ReadLine());
int k = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Number :{0} and Index :{1}",InsertNum,k);
while (length>=k)
{
LinearArr[length + 1] = LinearArr[length];
length = length - 1;
}
LinearArr[k] = InsertNum;
length = length + 1;
Console.ReadLine();
Change your while loop condition from:
while (length >= k)
to:
while (length > k)
as in current condition it will access last index which is not in the array.
Another mistake is your array is of size of value in the length variable:
LinearArr[length]
and in while loop you are accessing index greater then array length:
LinearArr[length + 1] = LinearArr[length]; // length +1 will give out of bounds error
When you define
int[] LinearArr = new int[length];
the valid indexes are 0..length-1. Therefore, this expression causes an out-of-range exception:
LinearArr[length + 1] = LinearArr[length];
neither length nor length+1 are valid indexes.
In order to fix this problem, allocate enough space in the array, and start moving elements at length-1:
int[] LinearArr = new int[length+1]; // Make space for an expansion
...
while (length > k) {
LinearArr[length] = LinearArr[length-1];
length--;
}
You may want to consider using a List<int> instead of an int[] as it provides an Insert function.
This is the code:
private static StringBuilder MakeRandomwords(string theWord)
{
var jumbleSb = new StringBuilder();
jumbleSb.Append(theWord);
int lengthSb = jumbleSb.Length;
for (int i = 0; i < lengthSb; ++i)
{
int index1 = (RandomGen.Next() % lengthSb);
int index2 = (RandomGen.Next() % lengthSb);
Char temp = jumbleSb[index1];
jumbleSb[index1] = jumbleSb[index2];
jumbleSb[index2] = temp;
}
return jumbleSb;
}
And this is the List that im using to build the scrambled words:
private void GetText()
{
_lengthaboveone = new List<string>();
for (int i = 0; i < _words.Count; i++)
{
string word = _words[i];
if (word.Length < 4) continue;
string first = word.Substring(0, 1);
string last = word.Substring(word.Length - 1, 1);
string middle = word.Substring(1, word.Length - 2);
_lengthaboveone.Add(middle);
_words[i] = first + MakeRandomwords(middle) + last;
}
_scrambledWords = _words;
}
In the end the List _scrambledWords contain over 1000 strings in each index a string of a word most of them scrambled but some of them are left the same as they were in the original.
The question is if there is something wrong with my MakeRandomwords ?
Could be it did scrambled the word and it was scrambled to how it was before ? So maybe i need to add something to the code that will keep scramble the word untill the word is scrambled by compraing it ot the original untill the word is scrambled ?
Take a look at the Fisher–Yates shuffle algorithm and implement the following pseudo code to achieve a good distribution of elements:
To shuffle an array a of n elements (indices 0..n-1):
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
To elaborate on Tim Schmelter's comment asking about RandomGen.Next(), in case you've been wondering: If you instantiate a new Random instance every time you're entering the for loop, the generated pseudo-random numbers will by nature be quite repetitive. This is due to the way that the Random class is implemented in the .NET framework. By reusing a shared instance like you do, one can avoid that issue.
This is not the problem here, though. In your algorithm, you're picking two random array elements and swapping them. It's highly likely that there are some array elements that never get selected this way. Thus, there's a good chance that some elements won't have changed their position in the array when you're done, which is why it doesn't look shuffled well.
Instead of randomly swapping two locations, swap every location to a random location... it will look more random because each element will have moved. With your current design there is a good chance some items won't ever move.
for (int i = 0; i < lengthSb; ++i)
{
int index1 = i;
int index2 = (RandomGen.Next() % lengthSb);
Char temp = jumbleSb[index1];
jumbleSb[index1] = jumbleSb[index2];
jumbleSb[index2] = temp;
}
I have a for loop that adds items in an array to a listView.
(It'll grab items on a webpage, remove anything after the ' in the string, then add it to the listView)
The error I am getting is: IndexOutOfRangeException was unhandled- Index was outside the bounds of the array
Here's the code I am using:
string[] aa = getBetweenAll(vid, "<yt:statistics favoriteCount='0' viewCount='", "'/><yt:rating numDislikes='");
for (int i = 0; i < listView1.Items.Count; i++)
{
string input = aa[i];
int index = input.IndexOf("'");
if (index > 0)
input = input.Substring(0, index);
listView1.Items[i].SubItems.Add(input);
}
The error occurs on this line: string input = aa[i];
Anything I did wrong? How can I fix this issue so it'll stop happening? Thanks!
If you're wondering the code for the getBetweenAll method is:
private string[] getBetweenAll(string strSource, string strStart, string strEnd)
{
List<string> Matches = new List<string>();
for (int pos = strSource.IndexOf(strStart, 0),
end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1;
pos >= 0 && end >= 0;
pos = strSource.IndexOf(strStart, end),
end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1)
{
Matches.Add(strSource.Substring(pos + strStart.Length, end - (pos + strStart.Length)));
}
return Matches.ToArray();
}
Your looping the elements of 'listView1'
If the item count of listView1 exceeds the number of elements of the string array 'aa' you will get this error.
I would either change the loop to be
for( ..., i < aa.Length, ...)
or inside your for loop put an if statement to make sure that you are not exceeding the elements of aa. (although, I doubt this is what you want to do).
for (int i = 0; i < listView1.Items.Count; i++)
{
if( i < aa.Length)
{
string input = aa[i];
int index = input.IndexOf("'");
if (index > 0)
input = input.Substring(0, index);
listView1.Items[i].SubItems.Add(input);
}
}
Well it is simple, your ListView.Items.Count is bigger then aa.Length. You need to make sure they have the same size.
Your for loop should be changed to
for (int i = 0; i < aa.Length; i++)
Also, when you do the below line, make sure the index matches.
listView1.Items[i].SubItems.Add(input);
Because of your above error, it seems it does not match, You might be better off looping through your list view to find a matching ListView Item and then maniuplate it.