简体   繁体   中英

Search sentence in text file c#

I want to make a program to search for a sentence but my program can only search for 1 character not a whole sentence. I don't want to use Contains . So from here what part of code should I edit? I think my loop have a problem but I can't figure it out

class Readfile
{
    public void readf()
    {
        int j;
        int i = 0;
        int flag = 0;
        Console.WriteLine("Enter sentence : ");
        string str = Console.ReadLine();
        char[] c1 = str.ToCharArray();
        string filename = @"C: \Users\jan31\Desktop\matthew\text.txt";
        using (StreamReader sr = new StreamReader(filename))
        {
            string str2 = sr.ReadToEnd();
            char[] fs = str2.ToCharArray();
            for (i = 0; i < fs.Length; i++) //loop for file
            {
                if (flag == c1.Length)
                {
                    Console.WriteLine("found");
                    break;
                }
                else
                {
                    for (j = 0; j < c1.Length;) //loop for user string input
                    {

                        if (c1[j] == fs[i])
                        {
                            flag = flag + 1;
                            j++;
                            break;
                        }
                        else
                        {
                            flag = 0;
                            j = 0;
                            break;
                        }

                    }
                }

            }
        }
    }
}

In Line 33, remove the break statement and add i++ . I verified it works. The problem is, you are not progressing with your search when the first character matches.

I suggest that you move this post to https://codereview.stackexchange.com/ , which looks like a more appropriate place for code reviews.

Setting aside all the issues of using "contains" or RegEx (either of which would be better and likely faster), the issue in your loop is that you keep resetting the value of "j" back to zero in order to increment "i". Once you find an initial match, you need to increment both the subject and target to determine if the match holds for the next character.

    public void readf()
    {
        int j;
        int i;
        var flag = 0;
        var str = "BCD";
        var c1 = str.ToCharArray();

        var str2 = "ABBCDEF";
        var fs = str2.ToCharArray();
        for (i = 0; i < fs.Length; i++) //loop for file
        {
            if (flag == c1.Length) // All characters in the search string where found.
            {
                Console.WriteLine("found");
                break; 
            }
            for (j = 0; j < c1.Length;) //loop for user string input
            {

                if (c1[j] == fs[i + j]) // By evaluating i + j, you don't lose your place within the file. 
                {
                    flag = flag + 1;
                    j++;
                    continue;
                }
                flag = 0;
                break;
            }
        }  // End file loop
        Assert.AreEqual(flag, c1.Length);
    }

You'll want to verify your edge cases of course.

Yes, your loop is not correct. Please take a look as my solution bellow:

        for (i = 0; i < fs.Length; i++)
        {
            if (c1[0] == fs[i])
            {
                for (var j = 1; j < c1.Length;) //loop for user string input
                {
                    if (c1[j] != fs[i = j])
                    {
                        break;
                    }
                }

                Console.WriteLine("found");
                Console.ReadLine();
                return;
            }
        }

        Console.WriteLine("not found");
        Console.ReadLine();

So if you want to read a file and look for a string while reading it. Maybe bacous you file is to large to read it at once. Here is my solution.

    class Readfile
    {
        public static void readf()
        {
            Console.WriteLine("Enter sentence : ");
            string str = Console.ReadLine();
            string filename = @"C: \Users\jan31\Desktop\matthew\text.txt";
            Encoding fileEncoding = Encoding.Default;
            bool Found = false;
            using (Stream s = System.IO.File.OpenRead(filename))
            {
                int current;
                string ReadText = "";
                List<Byte> L = new List<Byte>();
                do
                {
                    current = s.ReadByte();
                    if (current != -1)
                    {
                        L.Add((Byte)current);
                        ReadText = fileEncoding.GetString(L.ToArray());
                        if (ReadText.Length > str.Length)
                        {
                            L.RemoveAt(0);
                            ReadText = fileEncoding.GetString(L.ToArray());
                        }
                        if (ReadText.Length == str.Length)
                        {
                            if (ReadText == str)
                            {
                                //Found it ##############
                                Found = true;
                            }
                        }
                    }
                } while ((current != -1) && !Found);
            }
            if (!Found) {
                //Not Found it ##############
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM