Hi guys. I'm in a beginning C# class and I' having trouble with an assignment. I need to make a windows forms application that takes English text from a text box, converts it to Pig Latin, and then returns it to a second text box.

The specifications are:

If a word starts with a vowel, add "way" to the end of the word.

If a word starts with a consonant, move the consonants before the first vowel to the end of the word and add "ay".

If a word starts with the letter Y, the Y should be treated as a consonant. If the Y appears anywhere else in the word, it should be treated as a vowel.

Keep the case of the original word whether it's uppercase (TEST), title case (Test), or lowercase (test).

Keep all punctuation at the end of the translated word.

Translate words with contractions. For example, "can't" should be "an'tcay".

Don't translate words that contain numbers or symbols. For example, 123 should be left as 123 and should be left as .

Check that the user has entered text before performing the translation.

If anyone can help it would be greatly appreciated!

Thanks!

Dani AI

Generated

A compact, practical approach that fits the original constraints is to tokenise the input by whitespace (so spacing is preserved), then decide per token whether to translate or leave it unchanged. 's Regex suggestion is useful here — use regex to get non‑whitespace tokens, treat an apostrophe as part of a word (so contractions are handled), and treat any token containing internal digits or symbols as non‑translatable (leave e‑mails, numbers, hyphenated words, etc. alone).

For translatable tokens extract a core of letters+apostrophes and any trailing punctuation. Find the first vowel using a,e,i,o,u and treat y as a vowel only when it is not the first character. If the word starts with a vowel append "way"; otherwise rotate the leading consonant cluster before the first vowel and append "ay". Preserve letter case for the three requested categories (ALL UPPER, Title Case, all lower) and reattach the trailing punctuation to the translated core.

Example C# implementation (helpers included):

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Linq;

public static class PigLatin
{
    public static string ConvertToPigLatin(string text)
    {
        if (string.IsNullOrWhiteSpace(text)) return text;
        var parts = Regex.Split(text, @"(\s+)");
        var sb = new StringBuilder();
        foreach (var part in parts)
        {
            if (Regex.IsMatch(part, @"^\s+$")) { sb.Append(part); continue; }
            int firstNon = -1;
            for (int i = 0; i < part.Length; i++)
            {
                char c = part[i];
                if (!(char.IsLetter(c) || c == '\'')) { firstNon = i; break; }
            }
            string core, trail;
            if (firstNon == -1) { core = part; trail = ""; }
            else
            {
                var rest = part.Substring(firstNon);
                if (rest.All(ch => !char.IsLetter(ch) && !char.IsDigit(ch) && ch != '\'')) { core = part.Substring(0, firstNon); trail = rest; }
                else { sb.Append(part); continue; }
            }

            var pig = TranslateCore(core);
            if (IsAllUpper(core)) pig = pig.ToUpperInvariant();
            else if (IsTitleCase(core)) pig = char.ToUpperInvariant(pig[0]) + pig.Substring(1).ToLowerInvariant();
            else if (IsAllLower(core)) pig = pig.ToLowerInvariant();

            sb.Append(pig + trail);
        }
        return sb.ToString();
    }

    static string TranslateCore(string w)
    {
        var s = w.ToLowerInvariant();
        string vowels = "aeiou";
        if (s.Length == 0) return s;
        if (vowels.IndexOf(s[0]) >= 0) return s + "way";
        int idx = -1;
        for (int i = 0; i < s.Length; i++)
        {
            char c = s[i];
            bool isV = vowels.IndexOf(c) >= 0 || (c == 'y' && i > 0);
            if (isV) { idx = i; break; }
        }
        if (idx == -1) return s + "ay";
        return s.Substring(idx) + s.Substring(0, idx) + "ay";
    }

    static bool IsAllUpper(string s) => s.Where(char.IsLetter).All(char.IsUpper);
    static bool IsAllLower(string s) => s.Where(char.IsLetter).All(char.IsLower);
    static bool IsTitleCase(string s)
    {
        var letters = s.Where(char.IsLetter).ToArray();
        if (letters.Length == 0) return false;
        return char.IsUpper(letters[0]) && letters.Skip(1).All(char.IsLower);
    }
}

Notes and pitfalls: avoid a naive String.Split(' ') because it loses multi‑space and line breaks and will mishandle punctuation. The above approach skips tokens containing internal symbols/digits (addresses, numbers) and preserves trailing punctuation and contractions. Hyphenated words are left unchanged by default (or they can be split and handled sub-word by sub-word if desired). On the UI side call the converter only after verifying the input is non‑empty and include unit tests for uppercase, title case, punctuation, numbers, URLs and contractions to catch edge cases.

Since this is an assignment, giving you the code wouldn't help you learn. But I will tell you how I'd go about some of the things.

String.Split() to get the individual words.
Regex to see if it contains numbers or symbols.
I'd check the case of the word and convert the 'translated' word to that case.

So, give it a try and tell us what errors you are getting.

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.