title: Random Word Generator #2
body: <p>So I made a pretty simple word generator program in c# that works relatively well. It generates a word based on the length defined by the user. </p>
  
  <p>The algorithm adds a consonant and then a vowel for each consecutive letter in the sequence, which isn't ideal, but works well enough for basic words. </p>
  
  <p>My only issue is that I told it to add a "u" to the letter sequence if a "q" shows up right before it, but no matter what I've done it makes the word at least 1 letter too long.</p>
  
  <p>I have marked my problem area with a star in the comment above it. Here is the code:</p>
  
  <pre><code>public void WordFinder()
  {
      string word = null;
      int cons;
      int vow;
      //counter
      int i = 0;
      bool isword = false;
      Random rnd = new Random();
      //set a new string array of consonants
      string[] consonant = new string[]{"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"};
      //set a new string array of vowels
      string[] vowel = new string[]{"a","e","i","o","u"};
      while (isword == false)
      {
          word = null;
          Console.WriteLine("Pick the length of a word");
          int num = Convert.ToInt32(Console.ReadLine());
          //set the counter "i" to 1
          i = 1;
          if (num%2 == 0)
          {
              while (i &lt;= num)
              {
                  if (num != 1)
                  {
                      // current consonant = random consonant
                      cons = rnd.Next(0, 20);
                      // get the consonant from the string array "consonant" and add it to word
                      word = word + consonant[cons];
                      // add 1 to counter
                      i ++;
                      //* if the consonant is "q"
                      if (cons == 12)
                      {
                          // add "u" right after it
                          word = word + vowel[4];
                          // add 1 to counter
                          i++;
                      }
                  }
                  vow = rnd.Next(0, 4);
                  word = word + vowel[vow];
                  i ++;
              }
          }
          if (num % 2 != 0)
          {
              while (i &lt;= num - 1)
              {
                  //repeat same code as done to even length
                  if (num != 1)
                  {
                      cons = rnd.Next(0, 20);
                      word = word + consonant[cons];
                      i ++;
                      if (cons == 12)
                      {
                          word = word + vowel[4];
                          i ++;
                      }
                  }
                  vow = rnd.Next(0, 4);
                  word = word + vowel[vow];
                  i ++;
              }
              // if the length is not equal to 1
              if (num != 1)
              {
                  // add a consonant to the end of the word
                  cons = rnd.Next(0, 20);
                  word = word + consonant[cons];
              }
              //if the length is 1
              else if (num == 1)
              {
                  // pick a vowel
                  vow = rnd.Next(0, 4);
                  word = word + vowel[vow];
              }
          }
          i = 1;
          Console.WriteLine(word);
          Console.WriteLine("Is this a word? (y/n)");
          string q = Console.ReadLine();
          q = q.ToLower();
          //if the answer is yes, then it is a word and end the loop
          if (q == "y" || q == "yes")
          {
              isword = true;
          }
          //if the answer is no try the loop again
          else if (q == "n" || q == "no")
          {
              isword = false;
          }
      }
  }
  // main method
  static void Main(string[] args)
  {
      Program prog = new Program();
      prog.WordFinder();
      //wait for user input
      Console.ReadLine();
  }
  }
  </code></pre>