C
C#2y ago
Ryan-T1412

❔ ✅ how to add items in a empty array in cs

.
21 Replies
Ryan-T1412
Ryan-T14122y ago
i want to store index numbers inside of an array if i could do that i will replace * with vowels
ero
ero2y ago
no need
MODiX
MODiX2y ago
Ero#1111
REPL Result: Failure
string sentence = "Wh*r* d*d my v*w*ls g*?";
string vowels = "eeioeo";

Console.WriteLine(uncensor(sentence, vowels));

static string uncensor(string sentence, string vowels)
{
var ret = sentence.ToCharArray();

for (int i = 0, j = 0; i < sentence.Length; i++)
{
if (sentence[i] == '*')
{
ret = vowels[j];
j++;
}
}

return new string(ret);
}
string sentence = "Wh*r* d*d my v*w*ls g*?";
string vowels = "eeioeo";

Console.WriteLine(uncensor(sentence, vowels));

static string uncensor(string sentence, string vowels)
{
var ret = sentence.ToCharArray();

for (int i = 0, j = 0; i < sentence.Length; i++)
{
if (sentence[i] == '*')
{
ret = vowels[j];
j++;
}
}

return new string(ret);
}
Exception: CompilationErrorException
- Cannot implicitly convert type 'char' to 'char[]'
- Cannot implicitly convert type 'char' to 'char[]'
Compile: 660.523ms | Execution: 0.000ms | React with ❌ to remove this embed.
ero
ero2y ago
hm? not sure where the issue is i mean you get the idea oh
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
string sentence = "Wh*r* d*d my v*w*ls g*?";
string vowels = "eeioeo";

Console.WriteLine(uncensor(sentence, vowels));

static string uncensor(string sentence, string vowels)
{
var ret = sentence.ToCharArray();

for (int i = 0, j = 0; i < sentence.Length; i++)
{
if (sentence[i] == '*')
{
ret[i] = vowels[j];
j++;
}
}

return new string(ret);
}
string sentence = "Wh*r* d*d my v*w*ls g*?";
string vowels = "eeioeo";

Console.WriteLine(uncensor(sentence, vowels));

static string uncensor(string sentence, string vowels)
{
var ret = sentence.ToCharArray();

for (int i = 0, j = 0; i < sentence.Length; i++)
{
if (sentence[i] == '*')
{
ret[i] = vowels[j];
j++;
}
}

return new string(ret);
}
Console Output
Where did my vowels go?
Where did my vowels go?
Compile: 637.737ms | Execution: 89.810ms | React with ❌ to remove this embed.
mindhardt
mindhardt2y ago
fuck you too modix
ero
ero2y ago
i mean what? \* literally isn't an escape sequence
mindhardt
mindhardt2y ago
Yes, discord formatting issue
ero
ero2y ago
no, it's just that your input doesn't make sense
MODiX
MODiX2y ago
Hin#5905
REPL Result: Success
static string uncensor(string sentence, string vowels)
{
Queue<char> queue = new(vowels.ToCharArray());
return Regex.Replace(sentence, @"\*", _ => queue.Dequeue().ToString());
}

uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo")
static string uncensor(string sentence, string vowels)
{
Queue<char> queue = new(vowels.ToCharArray());
return Regex.Replace(sentence, @"\*", _ => queue.Dequeue().ToString());
}

uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo")
Result: string
Where did my vowels go?
Where did my vowels go?
Compile: 564.041ms | Execution: 68.389ms | React with ❌ to remove this embed.
mindhardt
mindhardt2y ago
Noice ⚠️ this isn't optimal, just wanted to check, you better stick with Ero's option
ero
ero2y ago
i mean if you want perf...
mindhardt
mindhardt2y ago
I just don't like em loops
ero
ero2y ago
just a bad mindset
mindhardt
mindhardt2y ago
Prolly
MODiX
MODiX2y ago
Hin#5905
REPL Result: Success
string sentence = "Wh*r* d*d my v*w*ls g*?";
string vowels = "eeioeo";

static string uncensor(string sentence, string vowels)
{
var ret = sentence.ToCharArray();

int j = 0;
for (int i = 0; i < sentence.Length; i++)
{
if (sentence[i] == '*')
ret[i] = vowels[j++];
}

return new string(ret);
}

uncensor(sentence, vowels)
string sentence = "Wh*r* d*d my v*w*ls g*?";
string vowels = "eeioeo";

static string uncensor(string sentence, string vowels)
{
var ret = sentence.ToCharArray();

int j = 0;
for (int i = 0; i < sentence.Length; i++)
{
if (sentence[i] == '*')
ret[i] = vowels[j++];
}

return new string(ret);
}

uncensor(sentence, vowels)
Result: string
Where did my vowels go?
Where did my vowels go?
Compile: 572.447ms | Execution: 49.595ms | React with ❌ to remove this embed.
ero
ero2y ago
lol isn't that just the same thing i did
mindhardt
mindhardt2y ago
Almost, yes, just put j++ inside the indexer
ero
ero2y ago
static string uncensor(string sentence, string vowels)
{
return string.Create(sentence.Length, (Sentence: sentence, Vowels: vowels), static (span, state) =>
{
for (int i = 0, j = 0; i < span.Length; i++)
{
span[i] = state.Sentence[i] == '*' ? state.Vowels[j++] : state.Sentence[i];
}
});
}
static string uncensor(string sentence, string vowels)
{
return string.Create(sentence.Length, (Sentence: sentence, Vowels: vowels), static (span, state) =>
{
for (int i = 0, j = 0; i < span.Length; i++)
{
span[i] = state.Sentence[i] == '*' ? state.Vowels[j++] : state.Sentence[i];
}
});
}
@Hin felt like doing this and thought you might be intested lol https://paste.mod.gg/xtvolefazggw
Ryan-T1412
Ryan-T14122y ago
thank you @Hin your code looks more complicated 🙂 i didn't get it as a beginner i didn't know that i can use i and j in the same loop 😅 !close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.