+ 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);
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).
+ 2
Thank you! That actually cleared it up for me. I believe I was overthinking it. Much appreciated.
+ 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?
- 1
it pretty much compares the respective ASCII value of each character.