To reverse a string we convert it into an array of characters, then we use a for
loop to print the reversed string. But in the below program we are getting characters from the string without converting it into characters. I am really confused how it has happened. Variable str
is string then why str[i]
is returning a character?
public static void Main() {
string str = "hello mr singh";
string temp ="";
for (int i = str.Length - 1; i >= 0; i--) {
temp += str[i];
}
Console.WriteLine("Reverse string:" + temp);
Console.ReadLine();
}