C
C#3y ago
kyo

❔ 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
kyo
kyoOP3y 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
Shinigami3y ago
Hi So if the word is "good" You'd need it as G,o,O,d ?
kyo
kyoOP3y ago
yes
Shinigami
Shinigami3y ago
You could loop over the char array and divide every index number by 2
kyo
kyoOP3y 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
Shinigami3y ago
You need to add condition for when index is 0 as well
kyo
kyoOP3y ago
|| i == 0 ?
Shinigami
Shinigami3y ago
If index = 0 then upper Yeah
kyo
kyoOP3y 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
Shinigami3y ago
Sure
kyo
kyoOP3y 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
Shinigami3y ago
Yes If i % 2 is anything else than 0 then it it's odd
kyo
kyoOP3y ago
okay so what would the proper syntax for tolower in this situation? i tried
.ToLower(word[i]);
.ToLower(word[i]);
no clue
Shinigami
Shinigami3y ago
Do one thing Make the word into a char array Like this
kyo
kyoOP3y ago
it starts as a char array
Shinigami
Shinigami3y ago
Oh sry, then it's easy. This should work 1 sec
kyo
kyoOP3y ago
this is the error im pulling
Shinigami
Shinigami3y ago
Just do this Word[i].tolower()
kyo
kyoOP3y ago
ok no overload method, takes 0 srgs args*
Shinigami
Shinigami3y ago
?? Can you show the error
kyo
kyoOP3y ago
Shinigami
Shinigami3y ago
Oh i guess since it's already lowercase it's throwing that error
kyo
kyoOP3y ago
monkaS
Shinigami
Shinigami3y ago
Refer this
kyo
kyoOP3y ago
would i have to do like i => 'A' && i <= 'Z' oh okay thank you i will look at that
Angius
Angius3y ago
It's char.ToLower('A') not 'A'.ToLower()
Shinigami
Shinigami3y ago
Bingo, sry this is the way to go @apparition
kyo
kyoOP3y 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]);
}
}
}
kyo
kyoOP3y ago
got it 🙂 thank you for your help shinigami and zzzzzzzzzzzzzz even tho i didnt see ur message hhaha
Shinigami
Shinigami3y ago
Np 😉
kyo
kyoOP3y ago
now how do i set this question as solved?
Shinigami
Shinigami3y ago
/close or something like that Not really sure, I'm new too
kyo
kyoOP3y ago
okok thank you again, appreciated very much
Pobiega
Pobiega3y ago
/close
PresidentNotSure
$close maybe?
MODiX
MODiX3y ago
Use the /close command to mark a forum thread as answered
PresidentNotSure
Nope
kyo
kyoOP3y ago
for some reason it keeps saying “the application did not respond” when i try to /close
Pobiega
Pobiega3y ago
the bot is having issues. its fine, just leave it
MODiX
MODiX3y 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
Cattywampus3y ago
fun homework you got there.. just answering for fun
kyo
kyoOP3y ago
your answer is great, unfortunately, we arent allowed to use var
Angius
Angius3y 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
kyo
kyoOP3y ago
i Naruhodo
Accord
Accord3y 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.

Did you find this page helpful?