I this program when input is catch-22 relevant output is "not found". Please fix the error.
Also whether it is possible to use Array.IndexOf in this program.
using System;
using System.Collections; //needed to do case insensitive search
public class DebugSix04
{
public static void Main()
{
String[] books = { "Rich Dad", "Poor Dad", "Catch-22", "Harry Potter", "Programming Using C#", "Wizard of Oz", "The Deep" };
IComparer comparer = new CaseInsensitiveComparer(); //lower case input will be realized as upper case letter
Array.Sort(books, comparer);
Console.Write("What book are you looking for? ");
string enterBookName = Console.ReadLine();
int x = Array.BinarySearch(books, enterBookName, comparer);
if (x < 0)
Console.WriteLine("{0} not found", enterBookName);
else
Console.WriteLine("Yes, we carry {0}", books[x]);
Console.ReadKey();
}
}
Loading
Posted Oct 4, 2012, 10:59 AM
Thank you for your help.
VulpesPosted Oct 4, 2012, 10:28 AM
The only thing I can think of is that it's something to do with the culture you're in. Try placing the highlighted line just after the Main() method starts and see if it makes any difference:
// etc
It wouldn't be sensible to use Array.IndexOf here because it doesn't have an overload to control the comparer. You'd therefore have to call it multiple times with all possible case combinations of the book name which is impractical.
Milind ThakkarPosted Oct 4, 2012, 2:07 AM