Answer to Question #62922 in C# for Peter

Question #62922
User entered firstName at the start of the program. This part allows them to then search the name they entered, but it is case sensitive, so if I entered 'James' for the name and then search 'james', it doesn't display info. How do I fix please?

Console.Write("Search for a student: ");
string searchedName = Console.ReadLine();

for (int i = 0; i < studentSize; i++)
{
if (firstName[i].Contains(searchedName))
{
Console.WriteLine();
Console.WriteLine("Name: \t\t\t" + firstName[i] + " " + lastName[i] + "\n" + "Maths Mark: \t\t" + maths[i]
+ "\n" + "English Mark: \t\t" + english[i] + "\n" + "Science Mark: \t\t" + science[i] + "\n" + "Average Mark: \t\t" + average[i]);
}
}
1
Expert's answer
2016-10-26T14:24:09-0400
Console.Write("Search for a student: ");
string searchedName = Console.ReadLine();

for (int i = 0; i < studentSize; i++)
{
if (firstName[i].ToLower()==searchedName.ToLower())
{
Console.WriteLine();
Console.WriteLine("Name: \t\t\t" + firstName[i] + " " + lastName[i] + "\n" + "Maths Mark: \t\t" + maths[i]
+ "\n" + "English Mark: \t\t" + english[i] + "\n" + "Science Mark: \t\t" + science[i] + "\n" + "Average Mark: \t\t" + average[i]);
}
}

Also you can initialize the variable, for example:
string searched = searchedName.ToLower();
above the for cycle and use it in the if condition

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS