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

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;
        }
    }
}
«1

Comments

  • AhmedAmr15AhmedAmr15 Member
    edited August 2020

    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");

           } 

        }

    }

  • 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;
            }
    
  • 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 2020
    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 2020

    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 2020

    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 2020

    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 2020

    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 2020

    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!");


        }

      }

    }

  • I don't get it why we use voids at all ;(.

    So here's my example without a void. I would really appreciate it if someone could explain me why we use voids!


    using System;

    using System.Collections.Generic; 


    namespace Learning_C_

    {

        class Program

        {

            static void Main(string[] args)

            {

               //appearance


               Console.ForegroundColor = ConsoleColor.Magenta; 

               Console.WindowHeight = 40; 

               Console.Title = ("yea"); 

               //code


               Console.WriteLine("Please write a short sentence."); 

               string sentence = Console.ReadLine(); 

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

               

               if(words > 1)

               {

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

               }


               else if(words == 1)

               {

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


               }


               else if(words == 0)

               {

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

               }


               





               



              

              Console.ReadKey();       

            }


            


            

            }

            

  • My Code

    
    
    namespace My_Awesome_Program
    {
        class Program
        {
            
            static void Main(string[] args)
            {
               
                Console.WriteLine("Type in a sentence: ");
    
    
    
               string sentence = Console.ReadLine();
    
    
               int wordCount = Words(sentence);
    
    
               Console.WriteLine("there are " + wordCount + " words in that sentence" );
              
             
             // Wait Before Closing 
            Console.ReadKey();
           } 
    
    
            static int Words(string sentence)
            {
                int wordCount = sentence.Split(' ').Length;
    
    
                return wordCount;
            }
    
    
        }
    }  
    
  • Mo_Mo_ Member

    Heres my solution. Definetely didnt go insane just because i forgot to write 'Console.ReadKey();' at the end lol.

    using System;
    
    
    namespace Brackeys_Challenge_6
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Getting sentence
              
               Console.WriteLine("Please input a sentence: ");
               string sentence = Console.ReadLine();
               
               // Splitting and printing
               
               int amount = WordCounter(sentence);
               Console.WriteLine("Your sentence contains " + amount + " words.");
    
    
               // Wait 
               Console.ReadKey();
            }
            
    
    
            static int WordCounter(string sentence) {
                int amount = sentence.Split(' ').Length;
                return amount;
            }
        }
    }
    


  • SharyShary Member

    Ok, here is mine.... a little bit diffrent cause i didn't check the tip:

    using System;
    
    namespace MethodsAndFunction
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Please write a sentence:");
                string sentence = Console.ReadLine();
                int howMany = countWords(sentence);
    
                Console.WriteLine($"There are {howMany} words.");
                Console.ReadKey();
            }
    
            static int countWords(string sentenceIN)
            {
                int lenght = sentenceIN.Length;
                sentenceIN = sentenceIN.Trim();
                string[] myTable = sentenceIN.Split(" ", lenght);
                return myTable.Length;
            }
    
        }
    }
    
  • using System;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                wordCount();
                // wait before close
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\nPress Enter to exit..");
                Console.ReadKey();
            }
    
            static void wordCount()
            {
                Console.WriteLine("Enter you sentence: ");
                string sentence = Console.ReadLine();
                int count = sentence.Split(' ').Length;
                Console.WriteLine("\nThis sentence have " + count + " word(s).");
            }
        }
    }
    


  • NexNex Member

    Assignment 6:

    using System;
    
    namespace C__Assignment6
    {
        class Program
        {
            static void Main(string[] args)
            {
                char input;
                do
                {
                    Console.WriteLine("Enter a sentence: ");
                    Statement();
                    Console.WriteLine("\nDo you want to try again?");
                    Console.WriteLine("Press Y or N");
                    input = Convert.ToChar( Console.ReadLine() );
                }while(input != 'N');
    
                Console.WriteLine("Thank you for testing out my code :)\nPress any key to exit.");
                //Wait before closing;
                Console.ReadKey();
            }
    
            static void Statement()
            {
                string sentence = Console.ReadLine();
                int words = sentence.Split(' ').Length;
                int letters = sentence.Length;
                Console.WriteLine("There are " + words + " words in the above sentence");
                Console.WriteLine("There are " + letters + " letters in the above sentence");
                //return sentence;
            }
        }
    }
    


Sign In or Register to comment.