I'm supposed to take the user's input and re-print it into alternate capital letters. I took the string and converted it into a char array and I'm trying to do it by using the remainder of the position in the array.
The problematic lines are the y = letter.ToUpper() and y = letter.ToLower() lines which gives me the error "No overload for method 'ToUpper'/'ToLower' takes 0 arguments. I'm not sure why I'm getting the error even after looking at other people's examples.
static void Main(string[] args)
{
Console.Write("Enter anything: ");
String x = Console.ReadLine();
char[] array = x.ToCharArray();
for(int i = 0; i<array.Length; i++)
{
char letter = array[i];
char y;
if(i % 2 == 0)
{
y = letter.ToUpper();
Console.Write(y);
}
else if(i % 2 == 1)
{
y = letter.ToLower();
Console.Write(y);
}
}
}
You're calling char.ToLower
- which is a static method accepting the relevant character as a parameter, and optionally a CultureInfo
.
So you probably want:
y = char.ToUpper(letter);
and
y = char.ToLower(letter);
Note that your loop would be a lot simpler if you used the conditional operator:
for(int i = 0; i < array.Length; i++)
{
char letter = array[i];
char y = i % 2 == 0 ? char.ToUpper(letter) : char.ToLower(letter);
Console.Write(y);
}
See more on this question at Stackoverflow