Home > Software engineering >  Linear search in a list array
Linear search in a list array

Time:10-30

working on a project where I'm making a "blog" that puts the blog posts in to a string array that then gets transferred to a list array. This is my current code and I'd appreciate any help. I'm still new to c# and programming as a whole so unsure how to fix it.

The current problem is that in case 3 I get the error message : Operator '==' cannot be applied to operands of type 'string' and 'string[]' and the name "i" does not exist in the current context of the "the post is in the blog" codeblock.

I created a minimal reproducible example here and the only other functions the program otherwise has is to 1. Delete all blogposts and 2. to print out all blogposts with their name and content.

        bool minBool = true;
        List<string[]> blogPost = new List<string[]> { };
        string[] post = new string[2];

        while (minBool)
        {
            Console.WriteLine("\n\n\tWelcome to the blog!");
            Console.WriteLine("\n\t[1] - Write a blogpost");
            Console.WriteLine("\t[3] - Search for a blogpost");
            Console.Write("\n\tChoice:");
            Int32.TryParse(Console.ReadLine(), out int input);

            switch (input)
            {
                case 1:
                    Console.Write("\tName your post: ");
                    post = new string[2];
                    post[0] = Console.ReadLine();
                    Console.Write("\tWrite your post: ");
                    post[1] = Console.ReadLine();
                    blogPost.Add(post);

                    break;

                case 3:

                    string searchTerm = Console.ReadLine();
                    string result = "The blogpost doesn't exist";
                    foreach (string blog in blogPost)
                    {
                        if (searchTerm == blog)
                        {
                            result = $"\tThe post is in the blog: {post[i]}";
                            break;
                        }
                    }
                    Console.WriteLine(result);
                    break;

CodePudding user response:

The problem is that you are trying to compare the searchterm variable, which is a string, with an item of the blogpost variable, which is of type string[]. This will not work.
Instead you can do something like this:

foreach(var blog in blogPost)
{
   if(searchTerm == blog[0] || searchTerm == blog[1])
   {
      result = $"\tThe post is in the blog: {blog[0]}";   // Print blog name
      break;
   }
}

But this way your search term has to be the exact same as the name or content of the post. Which is not very handy to do quick searches. Instead of comparing the string exactly you can make use of the Contains() method:

if(blog[0].Contains(searchTerm) || blog[1].Contains(searchTerm))
{
   result = $"\tThe post is in the blog: {blog[0]}";   // Print blog name
   break;
}

Now you can search for blog posts by only providing a part of the name or content.
Be aware that only the first blog post which fulfills the search term will be printed to the console.

Consider creating a BlogPost class for saving the blog post inside a List. This way your code is more expressive and easier to handle:

public class BlogPost
{
   public string Name { get; }
   public string Content { get; }

   public BlogPost(string name, string content)
   {
      Name = name;
      Content = content;
   }
}

Then save it like this:

private List<BlogPost> _blogPosts = new List<BlogPost>();

[...]

case 1:
   Console.Write("\tName your post: ");
   var name = Console.ReadLine();
   Console.Write("\tWrite your post: ");
   var content = Console.ReadLine();
 
   _blogPosts.Add(new BlogPost(name, content));

Now your search looks something like this:

foreach (var blog in _blogPosts)
{
   if(blog.Name.Contains(searchTerm) || blog.Content.Contains(searchTerm))
   {
      result = $"\tThe post is in the blog: {blog.Name}";   // Print blog name
      break;
   }
}

With a blog post class you are now way more flexible. You can for example add an additional property for key words which should be used when searching for blog posts etc.

  • Related