Simple substring search (brute force)

心已入冬 提交于 2021-02-10 15:49:05

问题


I'm trying to make a simple substring search using the brute force technique but I'm getting an error which I can't see. I'm quite new to programming so please keep that in mind. The problem might be very simple.

using System;
using System.Collections;
using System.Collections.Generic;

namespace SubstringSearch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please enter some letters: ");
            string sequence = Console.ReadLine();
            Console.Write("Enter the sequence you want to search for: ");
            string pattern = Console.ReadLine();

            Console.WriteLine(Search(pattern, pattern.Length, sequence, sequence.Length));

            Console.ReadLine();
        }

        public static int Search(string pattern, int patternLength, string sequence, int stringLength)
        {
            int i;
            int j;

            if (stringLength >= patternLength)
            {
                for (j = 0; j <= (stringLength - patternLength); j++)
                {
                    for (i = 0; i < patternLength && pattern[i] == sequence[i + j]; i++);

                    if (i >= patternLength)
                        return j;
                    else
                        return -1;
                }
            }
            else
                return -1;
        }
    }
}

So I'm getting one error and one warning. First it tells me that not all code paths return a value ( in Search() ). I can't see why. Second I get a warning that my integer 'j' is unreachable in the first for-loop (at 'j++').

Please help! I'm sure the answer is quite simple, but I just can't see it.


回答1:


The issue seems to lie in your second for loop. Try this:

 if (stringLength >= patternLength)
        {
            for (j = 0; j <= (stringLength - patternLength); j++)
            {
                for (i = 0; i < patternLength && pattern[i] == sequence[i + j]; i++)
                {
                    if (i >= patternLength)
                        return j;                       
                }
            }
        }
 return -1;

That should remove all warnings and errors and compile. Why are you not using the .Contains() method?

Contains

True if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.




回答2:


As far as I can tell, the error you're getting is because if the first 'for' loop didn't run even once then you would not hit a return statement. It might be unlikely/impossible but you still have to account for it. The solution to this is to remove the 'else' at the end so that if it gets that far it'll definitely hit the 'return -1'.




回答3:


the code route of no return is when stringLength = patternLength.




回答4:


Replace

Console.WriteLine(Search(pattern, pattern.Length, sequence, sequence.Length));

with

sequence.IndexOf(pattern);

And get rid of your Search function. You are rewritting (poorly) what's available in framework.



来源:https://stackoverflow.com/questions/10632874/simple-substring-search-brute-force

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!