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

2»

Comments

  • AkshayAkshay Member
    edited May 28

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;


    namespace classes

    {

      class Program

      {

        static void Main(string[] args)

        {

          meetAliens();

          //matte

          Console.ReadKey();

        }

        static void meetAliens()

        {

          Console.WriteLine("write down an sentence");

          string example = Convert.ToString(Console.ReadLine());

          Console.WriteLine("Tottal number of words are "+ example.Split(' ').Length);


           

        }

      }


  • ~~~

          string example = "sussy baka";



          //splits the string every time a custom char is found (define in parenth)

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



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


          // Wait before closing

          Console.WriteLine("\n\n\n");

          Console.WriteLine("Press any key to close.");

          Console.ReadKey();

    ~~~

  • edited June 7

    This is my homework :)

    I made some changes and combinations with previous members. Even as my code does not work perfectly I am happy with it right now :)

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

    using System;

    using System.Collections.Generic;


    namespace ConsoleApp1

    {

      class Program

      {

        static void Main(string[] args)

        {

          char input;

          do

          {

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

            CountWords();

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


          Console.ReadKey();

         }


        static void CountWords()

        {

          string word = Console.ReadLine();

          int numberOfWords = word.Split(' ').Length;

          int letters = word.Length;


           if (numberOfWords == 0)

          {

            Console.WriteLine("Please write a sentance!");


          }

          else if (numberOfWords == 1)

          {

            Console.WriteLine("There is " + numberOfWords + " word in sentence and " + letters + " letters.");

          } 

          

          else if (numberOfWords >= 2)

          {

            Console.WriteLine("There is " + numberOfWords + " words in sentence and " + letters + " letters.");

          }

        }

      }

    }

  • Victor16Victor16 Member

    This is my solution

  • marimari Member

    using System;

    using System.Collections.Generic;


    namespace my_awful_program

    {

        class Program

        {

            static void Main(string[] args)

            {

                // Console mod

                Console.Title = "Skynet";

                Console.ForegroundColor = ConsoleColor.Green;

                Console.WindowHeight = 40;



                Console.WriteLine("Put in your phrase ");

                string str = Console.ReadLine();

                Console.Write("Your phrase is " + StrLenght(str) + " words long");



                Console.ReadKey();

            }



            static int StrLenght (string str){


                    int lenght = str.Split(' ').Length;

                    return lenght;


            }

        }

    }

  • Matija8Matija8 Member

    Here is mine!i realized that you don't have to use mehods beacuse you can do it faster!

  • using System;
    
    
    namespace VS_CODE
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                Console.WriteLine("Write a sentence.");
    
    
                int wordNum = WordCount(Console.ReadLine());
    
    
                if(wordNum == 1)
                {
                    Console.WriteLine("This sentence has " + wordNum + " word!");
                }else{
                
                    Console.WriteLine("This sentence has " + wordNum + " words!");
                }
    
    
                Console.ReadKey();
    
    
            }
    
    
            static int WordCount (string sentence)
            {
                int wordNum = sentence.Split(' ').Length;
                return wordNum;
            }
        }
    }
    
  • TurtleDevTurtleDev Member
    edited July 14

    namespace My_Wholesome_Program

    {

        class Program

        {

            

            static void Main(string[] args)

            { 

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

                string words = Console.ReadLine();  

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

                Console.ReadKey();

            }

        }

    }


    Here's mine, didn't use any method to do it faster and cleaner. Why homework is fun when Brackeys puts it?

  • using System;

    using System.Collections.Generic;


    namespace c__test_projects

    {

        class Program

        {

            static void Main(string[] args)

            { //create a method that counts the number of words in a sentences

                

                Console.WriteLine("enter a sentence: \n");

                countingwords();


               // wait before closing 

             Console.ReadKey();

            }

            static void countingwords ()

            {

                

                string sentence;

                sentence = Console.ReadLine();

                Console.WriteLine("there's "+ sentence.Split(' ').Length + " words in this sentence");

                


            }

        }

    }

Sign In or Register to comment.