Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
What is the code for counting the similarity within a text file when a user enter a name in my program?

The program is a family tree that ask the user to type the family member first and last name and the result will count the list of members that has the same last name within the text file and display the amount in a messagebox. So far I've 50 members within a *.txt.
Posted
Updated 19-May-14 9:53am
v2
Comments
PIEBALDconsult 19-May-14 17:24pm    
It looks like the solutions assume one name per line; is that a correct assumption? I would likely read the whole file into one string and use a Regular Expression to count the Matches.

C#
int matchCount = File.ReadAllLines("filepath").Count(x => x.Contains("yourSearchstr"));
MessageBox.Show(matchCount.ToString());
 
Share this answer
 
Schematically:

- Open the text file
- Read it line-by-line and build a list of last names
- You then have a list of last names, against which you will be able to write things like
int numberOfPeopleWithLastNameSnow = list.Where(s => s == "Snow").Count();

- The only part left is deciding how you are going to handle user inputs.

Good luck. With more specific questions would come more specific answers.
 
Share this answer
 

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