How to get an output of multiple characters set in a string? C#
Console.WriteLine("Write your password and I will tell you how long it is");
string password = Console.ReadLine();
int howLong = password.Length;
int check = password.IndexOf("!");
int check1 = password.IndexOf("?");
if (password.Contains("!"))
{
Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ! positioned in the {check}th place!");
}
else if (password.Contains("?")){
Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ? positioned in the {check1}th place!");
}
else if (password.Contains("? !"))
{
Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ? positioned in the {check1}th place and a symbol ! positioned in the {check}th place!");
}
else
{
Console.WriteLine($"Your password is {howLong} characters long");
}
In the code above, everything works fine, if you find a char ''?'', it will print out the intended sentence, same goes with a char ''!'' and if theres no ''!'' or ''?'', then the ''else'' data type will execute. But how do I make execute ''else if'' data type if both of these symbols are found in the same string? As you can see in the code above, I added an option for that as well, but it never executes. If I put, for example, ''asdf!sds?sds'', the data type ''if'' will execute which only states the position of character ''!''. Why?
First do a check for both then for single occurrences.
Console.WriteLine("Write your password and I will tell you how long it is");
string password = Console.ReadLine();
int howLong = password.Length;
int check = password.IndexOf("!");
int check1 = password.IndexOf("?");
if (password.Contains("!") && password.Contains("?")){
Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ? positioned in the {check1}th place and a symbol ! positioned in the {check}th place!");
} else if (password.Contains("!"))
{
Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ! positioned in the {check}th place!");
}
else if (password.Contains("?")){
Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ? positioned in the {check1}th place!");
}
else
{
Console.WriteLine($"Your password is {howLong} characters long");
}
this is more compact
Console.WriteLine("Write your password and I will tell you how long it is");
string password = Console.ReadLine();
int howLong = password.Length;
int check1 = password.IndexOf("!");
int check2 = password.IndexOf("?");
if (check1 >= 0 && check2 >= 0) Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ? positioned in the {check2}th place and a symbol ! positioned in the {check1}th place!");
else
{
if (check1 > 0) Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ! positioned in the {check1}th place!");
else if (check2 > 0) Console.WriteLine($"Your password is {howLong} characters long and I also found a symbol ? positioned in the {check2}th place!");
else Console.WriteLine($"Your password is {howLong} characters long");
}
and this is more compact and generic
var symbols = new string[] {"!","?","@"};
string password = Console.ReadLine();
int howLong = password.Length;
Console.WriteLine($"Your password is {howLong} characters long");
foreach (var symbol in symbols)
{
var index = password.IndexOf(symbol);
if(index >=0)
{
Console.WriteLine($"and found a symbol {symbol} positioned in the {index.ToString()}th place");
}
}