+ 2

Can someone help explain this?

So I found the correct output to this block of code but now I'm trying to figure out WHY and HOW. I understand everything until it gets to the- foreach (char c in y){if(c>='f'){x+=c} - the output ended up being sololrn. I guess I don't understand where we're getting c y and 'f' from. Any help would be appreciated. Thank you. string x = ""; string y = "sololearn"; foreach(char c in y){ if(c>='f'){ x+=c; } } Console.Write(x);

30th Mar 2017, 3:45 PM
Carl Jones
Carl Jones - avatar
4 Answers
+ 5
c>='f' means characters after 'f'. From "sololearn", take characters after 'f' i.e. s,o,l,o,l,r,n (e,a come before f).
30th Mar 2017, 4:16 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 2
Thank you! That actually cleared it up for me. I believe I was overthinking it. Much appreciated.
30th Mar 2017, 4:21 PM
Carl Jones
Carl Jones - avatar
+ 1
string x = ""; string y = "sololearn"; // Iterate through the string // (string => array of characters) foreach(char c in y){ // check if char in c is greater than char 'f' //(eg: 'e' is greater than 'a') if(c>='f'){ // append char to x x+=c; } } Console.Write(x); still have doughts?
30th Mar 2017, 4:16 PM
Eranga
Eranga - avatar
- 1
it pretty much compares the respective ASCII value of each character.
30th Mar 2017, 4:19 PM
Edi Lipovac
Edi Lipovac - avatar