'c# Trying to loop through a string array , compare a string to find, and output the found string
A program to find a book in an array of books. This code doesn't find the book?
string aBookToFind;
string[] listOfBooks = { "Jane Eyre", "Catch22", "Dunkirk","Kidnapped", "Treasure Island" };
aBookToFind=Console.ReadLine();
for(int i=0;i<5;i++){
Console.WriteLine("Book : "+listOfBooks[i]);
if (aBookToFind == listOfBooks[i])
{Console.WriteLine("Found Book "+ listOfBooks[i];
}
}
Solution 1:[1]
There is a missing parentheses at line 7 before the ";"
{Console.WriteLine("Found Book "+ listOfBooks[i];
After fixing this and running the code with the Input: Catch22
The Output was:
Book : Jane Eyre
Book : Catch22
Found Book Catch22
Book : Dunkirk
Book : Kidnapped
Book : Treasure Island
So I'd say other than the missing ")" it's working as expected. If you expected another output, maybe add it to your question for a more precise answer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fabio Henrique |
