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.

Dani AI

Generated

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:

  • Decide what “word” means (letters only? allow hyphens/apostrophes? case-sensitive?).
  • Obtain a curated word list (plain text).
  • Load it once into memory as a case-insensitive HashSet<string> for O(1) lookups.
  • Normalize the input token (Unicode normalization, trim, strip unwanted punctuation) and then check membership.

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:

  • For contractions/hyphenation allow specific characters when building token (adjust the char predicate).
  • Plurals and inflections require stemming or a richer spell-check library (NHunspell or a platform spell API) if exact-lookup is insufficient.
  • For large datasets, keep the HashSet in a shared/static cache, not rebuilding it per request.
  • Language and locale matter: use a word list appropriate to the target language and normalize consistently.

Recommended Answers

All 7 Replies

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
commented: Helpful first post. +7

"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?

you can use as i said a text file

you can use this http://tny.cz/c81a40ad

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;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.