Hey everyone. In stumped. How would you take a string and verfiy it is a word?
Example:
String a = sldkjf
String b = dog
The results should be, "dog" is a word and "sldkjf" is not.
Any direction would be greatly appreciated.
Hey everyone. In stumped. How would you take a string and verfiy it is a word?
Example:
String a = sldkjf
String b = dog
The results should be, "dog" is a word and "sldkjf" is not.
Any direction would be greatly appreciated.
A practical, reliable approach is to treat the problem as a membership test against an electronic dictionary. As noted, a source of valid words is required; 's text-file idea is the simplest starting point, and 's regex-based cleanup is a good complement (use regex only to normalize tokens before lookup).
Recommended workflow:
Example implementation (load once, then test tokens):
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
static HashSet<string> LoadDictionary(string path)
{
var dict = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var line in File.ReadLines(path))
{
var word = line.Trim();
if (word.Length > 0) dict.Add(word);
}
return dict;
}
static bool IsWord(string input, HashSet<string> dict)
{
if (string.IsNullOrWhiteSpace(input)) return false;
var normalized = input.Normalize(System.Text.NormalizationForm.FormC);
var token = new string(normalized.Where(char.IsLetter).ToArray());
return token.Length > 0 && dict.Contains(token);
} Notes and troubleshooting:
token (adjust the char predicate). Jump to Post— cgeier 187Question 1: How do you know "dog" is a word?
Question 2: If you didn't know if "dog" was a word or not, how would you find out?
Jump to Post— Alejo.Book 7you could get a text file with a lot of words in it and then check for a specific word
System.IO.StreamReader reader = new System.IO.StreamReader("FILEPATH"); String text = reader.ReadToEnd(); if (System.Text.RegularExpressions.Regex.IsMatch(text, textBox1.Text)) { BLABLA } else BLABLA
Question 1: How do you know "dog" is a word?
Question 2: If you didn't know if "dog" was a word or not, how would you find out?
you could get a text file with a lot of words in it and then check for a specific word
System.IO.StreamReader reader = new System.IO.StreamReader("FILEPATH");
String text = reader.ReadToEnd();
if (System.Text.RegularExpressions.Regex.IsMatch(text, textBox1.Text))
{
BLABLA
}
else
BLABLA
"Question 2: If you didn't know if "dog" was a word or not, how would you find out?"
I would look it up in a dictionary. Is there a C# class like a dictionary? Or is there another way I am missing?
Ohhhh. I see. Thank you Alejo.Book!
Yes. You need to have an "electronic" version of a dictionary that contains valid words. This electronic version can be a text file. Additionally, new words are added to dictionaries from time to time, so you may need to update your file from time to time.
Do you want to write this with nested loops, or will a canned solution using regular expressions do?
Here's the regex version:
// At the top of your source:
using System.Text.RegularExpressions;
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.