You should read the docs for string.
string name = "Karen";
foreach (char c in name)
{
Console.WriteLine(c);
}
var firstChar = name[0];
Console.WriteLine(firstChar);
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
You should read the docs for string.
string name = "Karen";
foreach (char c in name)
{
Console.WriteLine(c);
}
var firstChar = name[0];
Console.WriteLine(firstChar);
Because a string isn't a char as much as a double isn't an int. If you need a single character from a string then you need to decide which character you want and use indexing to get it.
string name = "Bob Smith";
char firstLetter = name[0];
Taking a closer look at your code it appears you're trying to read a single character in. ReadLine returns a string. Convert.ToChar just grabs the first character from that string, assuming it isn't empty. But you are assuming that the string input is a number because you're then going to try to convert it to an int which will fail if it isn't already an int. Without clearer understanding of what you're trying to do here (and this looks like homework so it is unclear how far into the material you are), it would be easier to just read in the string and convert it to an integer (safely). Then work with the int directly. You don't need characters here.
string input = Console.ReadLine();
int zhmara;
if (Int32.TryParse(input, out zhmara))
{
//zhmara is the integral value of the input, do your work now
};
Another issue I see is that you're building up a string in your loop. You then print out the string. But the next line you're trying to convert the string to a char. Why are you doing this?