Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

BRACKEYS - Solution to C# Tutorial 06 Challenge

BrackeysBrackeys Administrator

Hey everyone!

Here is my solution to the challenge from the C# Tutorial 06 (Methods) video that is out soon.

Have fun with it! 😀

using System;


namespace My_Awesome_Program
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a sentence: ");


            string sentence = Console.ReadLine();


            int wordCount = CountWords(sentence);


            Console.WriteLine("There are " + wordCount + " words in that sentence.");


            // Wait before closing
            Console.ReadKey();
        }


        static int CountWords (string sentence) {


            int wordCount = sentence.Split(' ').Length;


            return wordCount;
        }
    }
}

Comments

  • AhmedAmr15AhmedAmr15 Member
    edited August 30

    Well First To finish the Episode Here is my answer



    using System;

    using System.Collections.Generic;


    namespace My_C__Course

    {

        class Program

        {


            

            

            static void Main(string[] args)

            { 

                  Console.Title = "Word Counter";

                  Console.ForegroundColor = ConsoleColor.Red;


                  Console.WriteLine("Please Enter A Senctence");

                  Console.ForegroundColor = ConsoleColor.White;

                  string Senctence = Console.ReadLine();

                  Console.ForegroundColor = ConsoleColor.Red;

                 Console.WriteLine(("You're sentence have ") + Senctence.Split(' ').Length + " words");



                Console.ReadKey();

            }

           

        }

    }

  • AlpcAlpc Member

    It works so I'm glad of it


    using System;

    using System.Collections.Generic;


    namespace My_Awesome_Program

    {

        class Program

        {

            static void Main(string[] args)

            {

               Console.WriteLine("Enter a sentence:");

               sentence();          

               Console.ReadLine();

            }


           static void sentence()

           {

               string words = Console.ReadLine();

               int length = words.Split(' ').Length;

               Console.WriteLine("There are " + length + " words in that sentence");

           } 

        }

    }

  • BuhPoiBuhPoi Member

    Here's mine!

    ---------------------------------------

    using System;


    namespace brackeys_methods_challenge

    {

        class Program

        {

            static void Main(string[] args)

            {

                // Console Options

                Console.Title = "Brackeys Methods Challenge";

                Console.WindowHeight = 40;

                Console.ForegroundColor = ConsoleColor.White;


                // Program


                Console.WriteLine("Input a sentence.");

                string wordCount = wordsMethod(Console.ReadLine());

                Console.WriteLine($"Your sentence contains {wordCount} words.");


                // Wait before closing

                Console.ReadKey();

            }

            // Method

            static string wordsMethod (string sentence) {

            int wordCount = sentence.Split(' ').Length;

            return Convert.ToString(wordCount);

            }

        }

    }

  • Hi I did my HomeWork

    namespace fil
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                Console.Write("Please enter a sentence: ");
                string sentence = Console.ReadLine();
    
    
                int result = WordCalculator("My AweSome Program",sentence);
               
                if (result == 1)
                {
                    Console.WriteLine("Your Sentence has " + result + " word");
                }
                else if(result == 0)
                {
                    Console.WriteLine("Your sentence has no words");
                }
                else if(result >= 2)
                {
                    Console.WriteLine("Your Sentence has " + result + " words");
                }
            }
            static int WordCalculator(string name , string sentence)
            {
                Console.Title = "My Awesome Program";
                int result = sentence.Split(' ').Length;
                
                return result;
            }
    
  • KittenKitten Member

    Here's my code!


  • Here is my homework!

    using System;
    
    
    namespace Methods_E6
    {
        class Program
        {
            static void Main(string[] args)
            {
                int counter = WordCount();
                Console.WriteLine("There are " + counter +" words in the sentence!");
                Console.ReadKey();
            }
    
    
            static int WordCount()
            {
                Console.Write("Enter the sentence: ");
                string example = Console.ReadLine();
                int counter = example.Split().Length;
                return counter;
            }
        }
    }
    
    
    
  • CodenNinjaCodenNinja Member
    edited August 31
    using System;
    
    namespace CodeNinja
    {
      class Program
      {
    
        static void Main()
        {
          WordCount();
        }
    
        static void WordCount()
        {
          Console.Write($"Type a sentence and I will give you a word count!\n_____________________\n\n Sentence:: ");
          string Sentence = Console.ReadLine();
    
          int WordCountNum;
          WordCountNum = Sentence.Split(" ").Length;
    
          if (Sentence != string.Empty)
          {
            if (WordCountNum > 1)
            {
              Console.Write($"_____________________\n\nThere are {WordCountNum} words in your sentence. ");
            }
            else
            {
              Console.Write($"_____________________\n\nThere is only {WordCountNum} word in your sentence. ");
            }
          }
          else
          {
            Console.Write("_____________________\n\nYou did not type anything. ");
          }
    
          Console.Write("Press any key to continue.");
          Console.ReadKey();
          Console.Clear();
          WordCount();
        }
      }
    }
    


  • My solution, very fun to do these challenges. Keep up the work Brackeys.

    using System;


    namespace functions

    {

      class Program

      {

        static void Main(string[] args)

        {

          string wordcount;

          Console.WriteLine("Enter a sentence- ");

          wordlengthfinder(wordcount = Console.ReadLine());

        }

        static void wordlengthfinder(string wordcount)

        {

          Console.WriteLine("\nThis is how many words are there in your sentence- \n");

          Console.WriteLine(wordcount.Split(' ').Length);

          Console.ReadKey();

        }

      }

    }

  • Here's mine. My Sentence() method didn't work until i put some code inside the ()s.


    using System;


    namespace My_Program

    {

      class Program

      {

        static void Main(string[] args)

        {

          Sentence(Console.Title = "Word_Counter");


          Console.ReadKey();

        }


        static string Sentence(string s)

        {

          Console.Write("Enter a sentance: ");


          s = Console.ReadLine();


          int n = s.Split(" ").Length;


          Console.WriteLine("There are " + n + " words in the sentence.");


          return s;

        } 

      }

    }

  • Here is my solution :D


    using System;


    namespace Duck

    {

        class Program

        {

            static void Main(string[] args)

            {

             string sentence;

             Console.WriteLine("Write a sentence: ");

             sentence = Console.ReadLine();

             Console.WriteLine("----------------");

             Console.WriteLine("There are " + wordCount(sentence) + " words in that sentence :3");


             Console.ReadKey();  

            }


            static int wordCount(string sentence)

            {

                int words = 0;

                for (int i = 0; i < sentence.Split(' ').Length; i++)

                {

                    words = i;

                }

                return words+1;

            }

        }

    }

  • using System;

    using System.Collections.Generic;


    namespace Learning_C_

      {

        class Program

        {

          static void Main(string[] args)

          {

            SentenceCounter();

            Console.ReadKey();

          }


          static void SentenceCounter()

          {

            Console.WriteLine("Write A Sentence And I Will Figure Out Howmany Words Are In The Sentence");

            string sentence = Console.ReadLine();


            int number = sentence.Split(' ').Length;

            Console.WriteLine("There Are " + number + " Words In Your Sentence");

          } 

        }

      } 

  • CostineacsuCostineacsu Member
    edited September 1

    Here is my homework. I didn't use methods but it works well.

    using System;
    
    namespace Methods
    {
        class Program
        {
            static void Main(string[] args)
            {  
                Console.WriteLine("Write a sentence:");
                string sentence = Console.ReadLine();
                sentence.Split(' ');
                string[] words = sentence.Split();
                Console.Write("The sentence has " + wordcount.Length + " words");
                Console.ReadKey();
            }
        }
    }
    


  • Here's what I came up with and it works somehow lol...

    using System;

    namespace My_Awesome_Program

    {

       class Program

       {

           static void Main(string[] args)

           {

               Console.WriteLine("Enter a Sentence:");

               int wordCount = CountSentence(Console.ReadLine());

               Console.WriteLine("There are " + wordCount + " words in the sentence.");

               Console.ReadKey();

           }

           static int CountSentence(string sentence)

           {

               int result = sentence.Split(' ').Length;

               return result;

           }

       }

    }

  • using System;


    namespace Learning

    {

      class Program

      {

        static void Main(string[] args)

        {

          Console.Title = "The Counting God";

          Console.ForegroundColor = ConsoleColor.Green;


          Console.WriteLine("Mortal, give me your words.");


          WordCount();


          Console.WriteLine("Press enter to terminate simulation.");

          Console.ReadLine();


        }


        static void WordCount()

        {

          string sentence = Console.ReadLine();

          int count = Convert.ToInt32(sentence.Split(' ').Length);

          Console.WriteLine("Verily, there were " + count + " words in that sentence!\nBEHOLD MY POWER AND BASK IN MY INFINITE GLORY!");

        }



      }

    }

  • Here is mine!

    using System;
    
    
    namespace CSharp {
        class Program {
            static void Main(string[] args) {
                
                countWords();
    
    
                // Close terminal on key stroke
                Console.ReadKey();
            }
    
    
            static void countWords() {
                
                Console.WriteLine("Please enter a sentence");
                string sentence = Console.ReadLine();
    
    
                int sentenceLength = sentence.Split(' ').Length;
    
    
                Console.WriteLine($"There are {sentenceLength} letters in that sentence");
            }
        }
    }
    
  • MossProphetMossProphet Member
    edited September 7

    Been following along, decided to start posting

    void Main(string[] args)
        {
          Console.WriteLine("Write a phrase to count up the words.");
          string phrase = Console.ReadLine();
          int wordcount = CountWords(phrase);
          Console.WriteLine("There are "+wordcount+" words in this phrase.");
    
        }
        static int CountWords(string phrase)
        {
          int wordcount = phrase.Split(' ').Length;
          return wordcount;
        }
    
  • NASAboyNASAboy Member
    edited September 11

    This is mine😁

    I love Brackeys.


  • Well, tried something different using functions, made a two number calculator, here it is


    using System;


    namespace CS_Practice

    {

       class Program

       {

           static void Main(string[] args)

           {

               Console.WriteLine("Hello! This is a two number Calculator.\n");

               while (true)

               {

                   Console.WriteLine("What would you like to do?\n");

                   Console.WriteLine("Addition (a), Subtraction (s), Multiplication (m), Division (d).\n");

                   Console.WriteLine("Enter the letter in ( ) to select: ");

                   string selection = Console.ReadLine();

                   Console.WriteLine("\n");





                   if (selection == "a")

                   {

                       Console.WriteLine("Enter first number : ");

                       double num01 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Console.WriteLine("Enter second number : ");

                       double num02 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Addition(num01, num02);

                       Console.WriteLine("\n");

                   }

                   else if (selection == "s")

                   {

                       Console.WriteLine("Enter first number : ");

                       double num01 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Console.WriteLine("Enter second number : ");

                       double num02 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Subtraction(num01, num02);

                       Console.WriteLine("\n");

                   }

                   else if (selection == "m")

                   {

                       Console.WriteLine("Enter first number : ");

                       double num01 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Console.WriteLine("Enter second number : ");

                       double num02 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Multiplication(num01, num02);

                       Console.WriteLine("\n");

                   }

                   else if (selection == "d")

                   {

                       Console.WriteLine("Enter first number : ");

                       double num01 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Console.WriteLine("Enter second number : ");

                       double num02 = Convert.ToDouble(Console.ReadLine());

                       Console.WriteLine();


                       Division(num01, num02);

                       Console.WriteLine("\n");

                   }


               }


           }


           static double Addition(double num01, double num02)

           {

               double result = num01 + num02;

               Console.WriteLine("Adding " + num01 + " with " + num02 + " .....\nResult = " + result);

               return result;

           }

           static double Subtraction(double num01, double num02)

           {

               double result = num01 - num02;

               Console.WriteLine("Subtracting " + num01 + " with " + num02 + " .....\nResult = " + result);

               return result;

           }

           static double Multiplication(double num01, double num02)

           {

               double result = num01 * num02;

               Console.WriteLine("Multiplying " + num01 + " with " + num02 + " .....\nResult = " + result);

               return result;

           }

           static double Division(double num01, double num02)

           {

               double result = num01 / num02;

               Console.WriteLine("Dividing " + num01 + " with " + num02 + " .....\nResult = " + result);

               return result;

           }


       }


    }

  • KrimationsKrimations Member
    edited September 24

    using System;



    namespace First_C__Program

    {

        class Program

        {

            static  void Main(string[] args)

            {

                //colors

                Console.ForegroundColor = ConsoleColor.Red;

                Console.Title = "Methods";



                Console.Write("Enter the Sentence : ");

                string input = Console.ReadLine();

                Console.WriteLine($"The sentence is of {wordCount(input)} words");


                //wait before closing 

                Console.ReadKey();

            }


            static int wordCount(string input)

            {

                int result = input.Split(' ').Length;

                return result;

            }

        }

    }

  • iksuddleiksuddle Member
    edited September 27

    Here is mine. Short and sweet

    using System;


    namespace _00_learning_project

    {

      class Program

      {

        static void Main(string[] args)

        {

          Console.WriteLine("Enter a sentence: ");

          string sentence = Console.ReadLine();


          Console.WriteLine("Your sentence has " + wordCount(sentence) + " words");


          //Wait before closing the program

          Console.ReadKey();

        }

         

        static int wordCount(string sentence)

        {

          return sentence.Split(' ').Length;

        }

      }

    }

  • using System;
    
    
    
    namespace My_Crazy_Project
    {
        class Program
        {
            static void Main(string[] args)
            {
                numOfSentence();
                Console.ReadKey();
            }
    
    
            static void numOfSentence()
            {
                Console.WriteLine("Enter a sentence : ");
                string sentence = Console.ReadLine();
                int num = sentence.Split(' ').Length;
                Console.WriteLine("There are "+ num +" of words in that sentence");
            }
            
        }
    }
    
    
    
    
  • far from correct, but it worked 🤣

  • Here is mine:

    using System;


    namespace Learn_Methods

    {

      class Program

      {

        static void Main(string[] args)

        {

          Console.Title = "TheRealInt";

          Console.ForegroundColor = ConsoleColor.Cyan;

          WordCal();

          Console.ReadKey();

        }




        static void WordCal()

        {

          Console.WriteLine("Please type here some text:");

          string inputText = Console.ReadLine();

          int inputWords = inputText.Split(' ').Length;

          int inputLetters;

          if (inputWords > 1) {

            inputLetters = inputText.Length - inputWords + 1;

          } else

          {

            inputLetters = inputText.Length;

          }

          Console.WriteLine("There are " + inputWords + " words in that sentence and " + inputLetters + " letters!");


        }

      }

    }

Sign In or Register to comment.