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

          }

        }

      }

    }

Sign In or Register to comment.