Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
in txt file i have a sentence: Please write any Sentence.
i must create function which has 2 parameter` first file address,the second is any word.
This function must return how many times did encounter the word in the sentence.
For example if i will call it in main like this`
Console.WriteLine(Counter(hasce,"any"));

it will print 1, because there is 1 word name of "any" in sentence.

What I have tried:

I've tried like this`
static public int Counter(string address,string word)
        {
            int myword= 0;
            string text = File.ReadAllText(address);
            if (word == text)
            {
                myword++;
            }
            
            return myword;
        }


static void Main(string[] args)
        {
           Console.WriteLine(Counter(address,"any"));
        }
Posted
Updated 12-Feb-18 20:47pm
v2
Comments
Maciej Los 13-Feb-18 2:36am    
It depends on what you mean "word"...
An address may have numbers and other signs: "post 444, SampleCity, Huge Street 45"
How many words is in that string?
Suren97 13-Feb-18 2:37am    
?
Suren97 13-Feb-18 2:56am    
4 words here is "Please write any sentence",this is a just text in txt file,i created txt file and wrote this small text in file, now i need write any word for example "any" and it will return 1, but with this function it printed 0. why?
Animesh Datta 13-Feb-18 2:37am    
so what is your problem in the code ? where are you stuck ?
Suren97 13-Feb-18 2:39am    
It's not working, it printed 0, but i have the word "any" in sentence

1 solution

PLease, read my comment to the question.

You have to Split[^] string on spaces, for example:
C#
string address = "Please write any Sentence";
string filter = "any";

int counter = address.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries).Count(x=>x==filter);
Console.WriteLine("{0}", counter);


[EDIT]
I forgot to mention about upper/lower letters! Note that comparison: ("any"=="Any") returns false.
I'd strongly recommend to read this:
How to: Compare strings (C# Programming Guide) | Microsoft Docs[^]
String.Compare Method (String, String, Boolean) (System)[^]

Good luck!


[EDIT #2]
Due to OP's comments, here is a complete example:
C#
void Main()
{
	
	string path = @"D:\data.txt";
	string find = "any";
	string[] lines =  File.ReadAllLines(path);
	int cTotal = 0;
	
	foreach(string line in lines)
	{
		cTotal += Counter(line, find);
	}
	
	Console.WriteLine("A '{0}' has been found {1} time", find, cTotal);
	
}

// Define other methods and classes here
public static int Counter(string line, string findword)
{
	int c= 0;
	
	string[] words = line.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries);
	foreach(string word in words)
	{
		if (string.Compare(word, findword, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase)==0)
		{
			c++;
		}
	}
	return c
}


More:
File.ReadAllLines Method (String) (System.IO)[^]
 
Share this answer
 
v4
Comments
Suren97 13-Feb-18 2:59am    
can you say another option without StringSplitOptions.RemoveEmptyEntries).Count(x=>x==filter)?
easier option?
Maciej Los 13-Feb-18 5:25am    
Split method returns an array of words:
string[] words = address.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries)
You have to loop through the array, compare word and return the number of equal words.
Suren97 13-Feb-18 5:36am    
i think you don't understand me.this is my address`
string address = "C:/Users/User/Desktop/data.txt"; in this file have written sentence "Please write any sentence".Now i must create function which has 2 parameter Counter(string address,string word).When i will call that function in Main` Counter(address,"write"),it must print 1, if instead of "write" i write "kvbkdfb" it must print 0.How can i write that function?
Maciej Los 13-Feb-18 5:47am    
I understand you very well, but you don't understand what i'm trying to tell you...
Suren97 13-Feb-18 5:59am    
how can i use it in function?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900