I need to read a string from standard input, perform some operations and meanwhile I need to copy some of input to char array. In C++ I could do it like this:
scanf("%s", &array[pos]);
which copies string s
to char array
at position pos
.
I need to do do this very quickly (olympic task).
Reading from one big while in C++ took 5 sec.
On C# I tried copying to array using string.elementAt()
in a loop but it took 70 seconds, which is way too much. Also, building one big string and than using string.ToCharArray() is a bad idea.
Any ideas how to do that?
char[] ciag = new char[1010001];
for(int x = 0 ; x < n ; x++){
line = Console.ReadLine();
sekw[x] = poz;
len = int.Parse(line.Split(' ')[0]); //length of string to copy
string znaki = line.Split(' ')[1]; //copied string
for (int j = 0; j < len; j++)
{
ciag[poz + j] = znaki[j]; //put into array. Perhaps slow.
}
poz += len;
ciag[poz++] = 'k'; //my stuff
}