-2

I was tasked with this assignement but I cannot figure out how to do it. I have a list of characters and I have to return a new list with the same contents except all the characters that are lowercase are changed to uppercases.

2 Answers2

2

I assume you mean you have a List<char> and want all entries uppercased, use LINQ and lambda expressions.

var chars = new List<char> {'a', 'b', 'c'};
var newList = chars.Select(Char.ToUpper).ToList();

Select invokes Char.ToUpper() on all entries and ToList() converts the result back to a list.

Sirhc
  • 518
  • 7
  • 13
  • Thanks, this might be what I'm looking for. On the microsoft documentation for Select, there is "IEnumerable squares = Enumerable.Range(1, 10).Select(x => x * x);" Is something akin to the (x => ...) necessary? – JeezyHungry May 07 '21 at 01:58
  • `chars.Select(x => Char.ToUpper(x))` is equivalent to `chars.Select(Char.ToUpper)` in this case, the latter is using a method group. See this question for more information https://stackoverflow.com/questions/886822/what-is-a-method-group-in-c – Sirhc May 07 '21 at 02:04
-1

i recommend reading this article, it simply says that you have to check if the char case is lower and then make it upper using the following code

string str1="Great Power";  
        char ch;  
        System.Text.StringBuilder str2 = new System.Text.StringBuilder();  

for(int i = 0; i < str1.Length; i++) {  
            //Checks for lower case character  
            if(char.IsLower(str1[i])) {  
                //Convert it into upper case using ToUpper() function  
                ch = Char.ToUpper(str1[i]);  
                //Append that character to new character  
                str2.Append(ch);  
            }
ArterPP
  • 41
  • 1
  • 7