C
C#2y ago
appi

❔ Homework Help. Make every element in char array alternate between upper and lowercase: LiKeThIs

I am working on an assignment where I am given a char[] words, and must convert it so that the characters in the array alternate between upper and lowercase. Any help on where to start?
46 Replies
appi
appiOP2y ago
Little update, I ran the program to check the random inputs a few times and it looks like all the inputs are fully uppercase, so I will just need to convert ever other index to lower. Seems that even indexes are upper and dd indexes should be lower.
Shinigami
Shinigami2y ago
Hi So if the word is "good" You'd need it as G,o,O,d ?
appi
appiOP2y ago
yes
Shinigami
Shinigami2y ago
You could loop over the char array and divide every index number by 2
appi
appiOP2y ago
public static void Test10(char[] word)
{
for(int i = 0 ; i < word.Length; i++)
{
if(i % 2 == 0)
{

}
}
}
public static void Test10(char[] word)
{
for(int i = 0 ; i < word.Length; i++)
{
if(i % 2 == 0)
{

}
}
}
this is what i have so far
Shinigami
Shinigami2y ago
You need to add condition for when index is 0 as well
appi
appiOP2y ago
|| i == 0 ?
Shinigami
Shinigami2y ago
If index = 0 then upper Yeah
appi
appiOP2y ago
okay so then I would just convert the even indexes to lower lemme check if expected outputs want 0 as upper or lower
Shinigami
Shinigami2y ago
Sure
appi
appiOP2y ago
okay i read it wrong, even indexes are upper, odd indexes are lower. so maybe i could do
i % 2 != 0
i % 2 != 0
to check that they are not even and then do ToLower?
Shinigami
Shinigami2y ago
Yes If i % 2 is anything else than 0 then it it's odd
appi
appiOP2y ago
okay so what would the proper syntax for tolower in this situation? i tried
.ToLower(word[i]);
.ToLower(word[i]);
no clue
Shinigami
Shinigami2y ago
Do one thing Make the word into a char array Like this
appi
appiOP2y ago
it starts as a char array
Shinigami
Shinigami2y ago
Oh sry, then it's easy. This should work 1 sec
appi
appiOP2y ago
this is the error im pulling
Shinigami
Shinigami2y ago
Just do this Word[i].tolower()
appi
appiOP2y ago
ok no overload method, takes 0 srgs args*
Shinigami
Shinigami2y ago
?? Can you show the error
appi
appiOP2y ago
Shinigami
Shinigami2y ago
Oh i guess since it's already lowercase it's throwing that error
appi
appiOP2y ago
monkaS
Shinigami
Shinigami2y ago
Refer this
appi
appiOP2y ago
would i have to do like i => 'A' && i <= 'Z' oh okay thank you i will look at that
Angius
Angius2y ago
It's char.ToLower('A') not 'A'.ToLower()
Shinigami
Shinigami2y ago
Bingo, sry this is the way to go @apparition
appi
appiOP2y ago
public static void Test10(char[] word)
{
for(int i = 0 ; i < word.Length; i++)
{
if(i % 2 != 0)
{
word[i] = char.ToLower(word[i]);
}
}
}
public static void Test10(char[] word)
{
for(int i = 0 ; i < word.Length; i++)
{
if(i % 2 != 0)
{
word[i] = char.ToLower(word[i]);
}
}
}
appi
appiOP2y ago
got it 🙂 thank you for your help shinigami and zzzzzzzzzzzzzz even tho i didnt see ur message hhaha
Shinigami
Shinigami2y ago
Np 😉
appi
appiOP2y ago
now how do i set this question as solved?
Shinigami
Shinigami2y ago
/close or something like that Not really sure, I'm new too
appi
appiOP2y ago
okok thank you again, appreciated very much
Pobiega
Pobiega2y ago
/close
PresidentNotSure
$close maybe?
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
PresidentNotSure
Nope
appi
appiOP2y ago
for some reason it keeps saying “the application did not respond” when i try to /close
Pobiega
Pobiega2y ago
the bot is having issues. its fine, just leave it
MODiX
MODiX2y ago
SlimStv#2835
REPL Result: Success
string test = "wewretqyeuewruwerrweui";
StringBuilder stb = new StringBuilder();
bool dummy = false;

foreach(var a in test)
{
stb.Append(dummy ? char.ToUpperInvariant(a) : a);
dummy = !dummy;
}

Console.WriteLine(stb);
string test = "wewretqyeuewruwerrweui";
StringBuilder stb = new StringBuilder();
bool dummy = false;

foreach(var a in test)
{
stb.Append(dummy ? char.ToUpperInvariant(a) : a);
dummy = !dummy;
}

Console.WriteLine(stb);
Console Output
wEwReTqYeUeWrUwErRwEuI
wEwReTqYeUeWrUwErRwEuI
Compile: 701.661ms | Execution: 50.137ms | React with ❌ to remove this embed.
Cattywampus
Cattywampus2y ago
fun homework you got there.. just answering for fun
appi
appiOP2y ago
your answer is great, unfortunately, we arent allowed to use var
Angius
Angius2y ago
Just use the actual type, then var is not magic
string foo = "hello";
string foo = "hello";
and
var foo = "hello";
var foo = "hello";
are the exact same
appi
appiOP2y ago
i Naruhodo
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server