-1

I am learning Dotnet c# on my own. how to find whether a given text exists or not in a string and if exists, how to find count of times the word has got repeated in that string. even if the word is misspelled, how to find it and print that the word is misspelled? we can do this with collections or linq in c# but here i used string class and used contains method but iam struck after that.

if we can do this with help of linq, how? because linq works with collections, Right? you need a list in order to play with linq. but here we are playing with string(paragraph). how linq can be used find a word in paragraph? kindly help.

here is what i have tried so far.

 string str = "Education is a ray of light in the darkness. It certainly is a hope for a good life. Eudcation is a basic right of every Human on this Planet. To deny this right is evil. Uneducated youth is the worst thing for Humanity. Above all, the governments of all countries must ensure to spread Education";
    for(int i = 0; i < i++)
    if (str.Contains("Education") == true)
    {
              
        Console.WriteLine("found");
        
    }
    else
    {
        Console.WriteLine("not found");
    }
  • Split sentence with space. It will return array of words you can check words there. But if mis-spelled then it will be typical. – Amit Verma Aug 20 '22 at 08:22
  • 1
    A string is just a 'collection' of characters, why it wouldn't work with linq? The question you should ask yourself is whether this is the best solution for your problem in terms of performance, memory usage, etc.. You might want to simply use existing string functions or regular expressions for this use case. – Isma Aug 20 '22 at 08:22
  • 1
    "even if the word is misspelled", do you consider "males" as a misspelling of "makes"? Deciding if a word is a misspelling is not trivial. – Martheen Aug 20 '22 at 08:45
  • @Martheen you are right. Deciding if a word is a misspelling is not trivial always. the example that i have given is not suitable to the question that i asked. let it be the below paragraph"Education is a ray of light in the darkness. It certainly is a hope for a good life. Eudcation is a basic right of every Human on this Planet.Above all, the governments of all countries must ensure to spread Education". here you can see education is misspelled as eudcation in 3rd line and it doesnt make sense. so how would you find the misspelled word here? –  Aug 20 '22 at 12:35
  • Think about how *you* can figure out if it's misspelled. It's because you have English vocabulary in your head. So use a collection of English words like https://wordnet.princeton.edu/ – Martheen Aug 20 '22 at 13:10

3 Answers3

4

You can make a string a string[] by splitting it by a character/string. Then you can use LINQ:

if(str.Split().Contains("makes"))
{
    // note that the default Split without arguments also includes tabs and new-lines
}

If you don't care whether it is a word or just a sub-string, you can use str.Contains("makes") directly.

If you want to compare in a case insensitive way, use the overload of Contains:

if(str.Split().Contains("makes", StringComparer.InvariantCultureIgnoreCase)){}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thank you so much. . but if the given word is misspelled then how to find it and print that the word is misspelled? –  Aug 20 '22 at 14:41
  • @marvel: thats a completely different question. You can google "fuzzy search C#". The Levenshtein distance algorithm is one way to find similar strings. Here is an example: https://stackoverflow.com/questions/9453731/how-to-calculate-distance-similarity-measure-of-given-2-strings – Tim Schmelter Aug 20 '22 at 14:50
1
string str = "money makes many makes things";
var strArray = str.Split(" ");
var count = strArray.Count(x => x == "makes");
Qwerty
  • 429
  • 2
  • 8
1

the simplest way is to use Split extension to split the string into an array of words.

here is an example :

var words = str.Split(' ');

if(words.Length > 0)
{
    foreach(var word in words)
    {
        if(word.IndexOf("makes", StringComparison.InvariantCultureIgnoreCase) != -1)
        {
            Console.WriteLine("found");
        }
        else
        {
            Console.WriteLine("not found");
        }   
    }   
}

Now, since you just want the count of number word occurrences, you can use LINQ to do that in a single line like this :

var totalOccurrences = str.Split(' ').Count(x=> x.IndexOf("makes", StringComparison.InvariantCultureIgnoreCase) != -1);

Note that StringComparison.InvariantCultureIgnoreCase is required if you want a case-insensitive comparison.

iSR5
  • 3,274
  • 2
  • 14
  • 13
  • 1
    OP said it's a paragraph, so it won't find `makes.`, `makes!`, `makes?` etc – Martheen Aug 20 '22 at 08:42
  • thank you so much @iSR5. but if the given word is misspelled then how to find it and print that the word is misspelled? –  Aug 20 '22 at 12:31