0

Ok so im trying to put together a little function that will allow a person to type out a sentence, and it will then search for words that sound like numbers, and then list those numbers!

For instance someone enters the following sentence - "This is just one test sentence, for the next section I will talk about two different things!"

When you look through you can see the word "one", the word "for" and the word "two", so the output should read - 142

It would use a list of words like this -

string[] strNumWords = {"zero", "one", "won", "two", "too", "to", "three",
                        "four", "for", "fore", "five", "six", "seven" , "eight",
                        "ate", "nine", "nein"};

Im not really sure of the logic of putting this into practice as im fairly new to coding (clearly lol), so any clues would be great appreciated.

Im not entirely sure if I should be using arrays, lists or Dictionaries for this type of stuff.

The output im getting is looking like this - 124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124124

Its repeating and its not in the correct order! Driving me insane!

string[] strNumWords = {"zero", "one", "won", "two", "too", "to", "three",
                        "four", "for", "fore", "five", "six", "seven" , "eight",
                        "ate", "nine", "nein"};

int[] codeNumber = new int[100];

string codeSentence = "This is just for a quick test to see if one of these four numbers will show up.";


for (int i = 0; i < codeSentence.Length; i++)
{
    if (codeSentence.Contains("one"))
    {
        Console.Write(1);

        codeNumber[i] = 1;

    }

    if (codeSentence.Contains("won"))
    {
        Console.Write(1);
        codeNumber[i] = 1;

    }

    if (codeSentence.Contains("two"))
    {
        Console.Write(2);
        codeNumber[i] = 2;

    }

    if (codeSentence.Contains("to"))
    {
        Console.Write(2);
        codeNumber[i] = 2;

    }

    if (codeSentence.Contains("for"))
    {
        Console.Write(4);
        codeNumber[i] = 4;

    }

}

Console.ReadKey();
Stiffeno
  • 3
  • 1
  • 2
    Look at your code. For each character in the sentence, you're doing the same thing over and over again: checking if the sentence contains certain words and outputting numbers if it does. Seems like you should instead break the sentence into words, then reconstruct the sentence with replacement words where necessary. – Rufus L Nov 21 '22 at 07:08
  • *"Im not really sure of the logic of putting this into practice"*. They are two different things. Work out the logic first, which takes no programming experience, then write code to implement the logic. When you write code without a clear idea of what it actually has to do, you end up with nonsense, like you posted above. If you can't explain the steps the code is supposed to perform, you should be writing code. – jmcilhinney Nov 21 '22 at 07:16
  • some tips, you might want to use `string.split` to divide your input into distinct words. Use a `Dictionary` to map each number word to a number (and use a stringcomparer when creating the dictionary to make it case insensitive), then add all the numbers to a `List`. – JonasH Nov 21 '22 at 07:18

1 Answers1

1

Your current code is looping through the sentence and doing the same thing over and over again n number of times, where n is the number of characters in the sentence.

Instead, you might want to break the sentence into words (possibly by splitting on the space character), then reconstruct the sentence with replacement words where necessary. We also might want to create a way to pair a list of words with a number, so we know which numbers should replace which words. We can use a dictionary for this, with a list of words as the key, and the number as a value for each pairing.

For example:

public static string ReplaceWordsWithNumbers(string input)
{
    // Each dictionary item holds a list of words as it's Key
    // and the number to replace them with as it's Value
    var numWordCombos = new Dictionary<string[], int> {
        { new[] { "zero" }, 0 },
        { new[] { "one", "won" }, 1 },
        { new[] { "two", "too", "to" }, 2 },
        { new[] { "three" }, 3 },
        { new[] { "four", "for", "fore" }, 4 },
        { new[] { "five" }, 5 },
        { new[] { "six" }, 6 },
        { new[] { "seven" }, 7 },
        { new[] { "eight", "ate" }, 8 },
        { new[] { "nine" }, 9 }
    };

    // Split our sentence into words
    string[] words = input.Split(' ');
    List<string> newWords = new List<string>();

    // Replace words when found in the Key of one of our dictionary items
    foreach (string word in words)
    {
        // See if any items in the dictionary contain this word in the Key array
        var wordNumPair = numWordCombos.FirstOrDefault(c => 
            c.Key.Contains(word, StringComparer.OrdinalIgnoreCase));

        // If no pairing was found, the wordNumPair Key will be null. 
        if (wordNumPair.Key == null)
        {
            // So just add the original word to our list
            newWords.Add(word);
        }
        else
        {
            // We found a pairing, so add the wordNumPair Value 
            // to our list instead of the original word
            newWords.Add(wordNumPair.Value.ToString());
        }
    }

    // Join the new list of words back into a sentence and return it
    return string.Join(" ", newWords);
}

Then in use you could do something like:

static void Main(string[] args)
{
    var sentence = "This is just for a quick test to see if one of these four numbers will show up.";

    Console.WriteLine(sentence);
    Console.WriteLine(ReplaceWordsWithNumbers(sentence));

    Console.ReadKey();
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Thank you all for the great input, will be looking into the code example posted closely and examine exactly how it works! Thanks heaps! – Stiffeno Nov 21 '22 at 07:40
  • @Stiffeno sorry I closed as duplicate, if you change the title to "Change text to abbreviated form with numbers" or something more generic that others will search for & reuse then I'll reopen. Good luck! – Jeremy Thompson Nov 21 '22 at 10:38