J.Cohen
J.Cohen

Reputation: 3

string array upper and lower letters change C#

I need to print an array of strings line by line, where the first line has to be with uppercase letters, the second line with lowercase letters and thats how it goes until the end of the array (I get the array from a text file);

When I try to use to use "chars[i].ToUpper" I get the error

"Error 1 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement C:\Users\Yonatan\Documents\Visual Studio 2013\Projects\Clab2\Clab2\ex3.cs 21 17 Clab2"

this is my code:

static void Main(string[] args)
{
    string fileContent = File.ReadAllText("FreeText.txt");
    string[] chars = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);


    for(int I =0;i<chars.Length;i++)
    {
        if (i % 2 != 0)
            chars[i].ToUpper;

    }
}

Upvotes: 0

Views: 2021

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460158

the first line has to be with uppercase letters,the second line with lowercase letters

You have to overwrite the string in the array with the upper/lower-case strings:

if (i % 2 != 0)
    chars[i] = chars[i].ToUpper(); 
else
    chars[i] = chars[i].ToLower(); 

Upvotes: 2

ToUpper is a method, not a property. Also you have to reassign it, because ToUpper will return a new string.

Try with this:

for (int i = 0; i < chars.Length; i++) 
{
    if (i % 2 != 0)
    {
        chars[i] = chars[i].ToUpper();
    }
    else
    {
        chars[i] = chars[i].ToLower();
    }
}

Upvotes: 1

deyu
deyu

Reputation: 329

Replace

chars[i].ToUpper();

to

chars[i] = chars[i].ToUpper();

Upvotes: 1

Related Questions