Build Web Apps with ASP.NET - Loops Review

I could not find helpful solutions to the Loops Review exercise for the Skill Path “Build Web Apps with ASP.NET”.
I hope this may help someone to resolve the first exercise “Loops through a piece of text and only prints words that start with the letter “a” to the console to create a poem.”.

Here is my take, hope to see some suggestions to make it much better or shorter :

using System;

namespace LoopsReview
{
  class Program
  {
    static void Main(string[ ] args)
        {
          //Loop through a piece of text and only prints words that start with the letter “a” to the console to create a poem.
          //Achieved without the .Split method
          //1. Let's define a story :
            string Story = "Justice Department prosecutors had reached out to Pence’s representatives to seek his testimony in the criminal investigation, according to people familiar with the matter. Pence’s team had indicated he was open to discussing a possible agreement with DOJ to provide some testimony, one person said./nThat request occurred before the department appointed Smith to oversee two Trump-related investigations, the January 6-related probe and another into alleged mishandling of classified materials found at the former president’s Mar-a-Lago residence.";
          // ... and print that story to the console :
          Console.WriteLine(Story);

          //2. Let's determine where our index starts and where it ends :
            int indexStart = 0;
            int indexEnd = Story.Length;

          //3. We should now loop through each of these indexes to see what they contain. So let's first create the loop 
            for (int i = indexStart; i < indexEnd; i++)
            {
                //4. Our loop is defined but we need conditions.
                //First, our loop needs to find spaces (' ') 
                //or make sure the word lenght is greated than 1, presumably a space  
                if (Story[i] == ' ' || i == Story.Length - 1)
                {
                    /*From there we could define a word lenght with "int wordLength = i - indexStart;"
                    But this would also print words with a single character such as "a" 
                    or include a trailing space at the end of the words.
                    To prevent that, we can check if the total lenght of the word is equal to 1 without space
                    if so, we want the value to be set to 0, else 1. 
                    The "? 1 : 0" conditional statement was not presented in the Codecademy course 
                    but we were often told to check Microsoft documentation which I did. 
                    I believe this can be achieved without this conditional but in a much longer fashion.
                    */  
                    int wordLength = i - indexStart + (i == Story.Length - 1 ? 1 : 0);

                    //Now we have an index to start and an index to end a word that we can pass to the substring method :
                    string word = Story.Substring(indexStart, wordLength);
                    
                    //Finally, we can add some conditions
                    //The first is to check the word contains the letter "a", that's the goal of the exercise
                    //We can go a bit further, let's say we want words longer than one character (i.e. "a") 
                    //and exclude words such as "and", "at", ... 
                    if (word.ToLower().StartsWith("a") && word.Length > 1 && word != "and" && word != "at")
                    
                            {
                                //We can finally request the word to be printed if it meets the above conditions 
                                Console.WriteLine(word);
                            }
  
                    //As for our loop, we want to shift our index start by one so that we move along the text
                    indexStart = i + 1;

                    //I hope this was helpful.
                }
            }
        }
  }
}

Nice, you tend to learn a lot by approaching a problem like you have. I like how you have itemised your thoughts in the comments (and like I do as though you’re speaking to someone :slight_smile: )

This is another way to do it which is shorter like you asked for. I don’t think it is completely fool proof though depending on input :thinking:

string Story = "Justice Department prosecutors had reached out to Pence’s representatives to " +
    "seek his testimony in the criminal investigation, according to people familiar with the matter. " +
    "Pence’s team had indicated he was open to discussing a possible agreement with DOJ to provide some " +
    "testimony, one person said./nThat request occurred before the department appointed Smith to oversee " +
    "two Trump-related investigations, the January 6-related probe and another into alleged mishandling " +
    "of classified materials found at the former president’s Mar-a-Lago residence.";

string[] words = Story.Split(" ");
foreach (var w in words)
{
    var word = w.ToLower();
    if (word.StartsWith("a") && word.Length > 1 && word != "and" && word != "at")
    {
        Console.WriteLine(word);
    }
}