5

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);
            }                
        }
    }
1
  • 2
    Just a side note. There is no need to convert the string to array. You can access it's single letters using index as well for(int i = 0; i<x.Length; i++) { x[i] ...`
    – VladL
    Apr 30, 2015 at 19:35

2 Answers 2

6

Unlike string, char does not have ToUpper() or ToLower() instance methods.

It does have static methods with those names, which is why you get a confusing error message.

Use char.ToLower(y).

0
6

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);
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.