none
Trim Start RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Zadatak2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Napišite program koji traži unos riječi sve dok se ne unese riječ "kraj".
                //Program ispisuje: riječi koje počinju slovom A, B, C, ostale riječi.

                List<string> rijeci = new List<string>();

                while (true)
                {
                    Console.WriteLine("Upišite riječi: ");
                    string rijec = Console.ReadLine();

                    if (rijec.ToLower() == "kraj")
                    {
                        break;
                    }
                    else
                    {
                        rijeci.Add(rijec);
                    }
                }
                List<string> rijeciKojePocinjuSaSlovomA = (  //WORDS START WITH LETTER "A"
                    from r in rijeci
                    where r.TrimStart().Contains("A")
                    select r).ToList();
                Console.WriteLine();
                Console.Write("Riječi koje počinju sa slovom \"A\" su: ");

               foreach (string r in rijeciKojePocinjuSaSlovomA)
                {
                    Console.Write(r + ",");
                }
                List<string> rijeciKojePocinjuSaSlovomB = ( //WORDS START WITH LETTER "B"
                     from r in rijeci
                     where r.TrimStart().Contains("B")
                     select r).ToList();
                Console.WriteLine();
                Console.Write("Riječi koje počinju sa slovom \"B\" su: ");

                foreach (string r in rijeciKojePocinjuSaSlovomB)
                {
                    Console.Write(r + ",");
                }
                List<string> rijeciKojePocinjuSaSlovomC = ( //WORDS START WITH LETTER "C"
                     from r in rijeci
                     where r.TrimStart().Contains("C")
                     select r).ToList();
                Console.WriteLine();
                Console.Write("Riječi koje počinju sa slovom \"C\" su: ");

                foreach (string r in rijeciKojePocinjuSaSlovomC)
                {
                    Console.Write(r + ",");
                }
                List<string> ostaleRijeci = ( //WORD START WITH ANY OTHER LETTER BUT "A", "B" OR "C".
                    from r in rijeci
                    where r.TrimStart(). //WHAT HAVE TO WRITTE HERE??
                    select r).ToList();
                Console.WriteLine();
                Console.Write("Ostale riječi: ");

                foreach (string r in rijeciKojePocinjuSaSlovomC)
                {
                    Console.Write(r + ",");
                }
                Console.ReadKey();
               
            }
        }
    }

    Wednesday, July 18, 2018 5:03 PM

All replies

  • 1) I don't think you'll need TrimStart() at all. Because all that TrimStart() is doing is to eliminate leading whitespaces, but with Contains("A") you're looking for capital A. So, it doesn't matter how much whitespaces come in front of your word. I would scrap that TrimStart().Conains() construct and use StartsWith() instead.

    2) In your last query, you're looking for "ostaleRijeci", that as per G*-Translate means "others". So, I would check for words that are neither part of A, B and C (= not Contains()).

    Maybe, you could do sth. like this:

    List<string> names = new List<string>() { "Anton", "Alois", "Axel", "Bertold", "Bart", "Bernd", "Detlef", "Dagobert", "Donald", "Rudolf", "Herbert" };
    var A = names.Where(x => x.StartsWith("A"))
        .Select(x => x).ToList();
    var B = names.Where(x => x.StartsWith("B"))
        .Select(x => x).ToList();
    var D = names.Where(x => x.StartsWith("D"))
        .Select(x => x).ToList();
    var others = names.Where(x => !A.Contains(x))
        .Where(x => !B.Contains(x))
        .Where(x => !D.Contains(x))
        .Select(x => x).ToList();
    
    A.ForEach(Console.WriteLine);
    Console.WriteLine();
    B.ForEach(Console.WriteLine);
    Console.WriteLine();
    D.ForEach(Console.WriteLine);
    Console.WriteLine();
    others.ForEach(Console.WriteLine);
    
    

    wizend 

     

    Wednesday, July 18, 2018 6:26 PM
  • Hi Fani_Prizmic,

    Thank you for posting here.

    For your question, please try the code below.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    namespace Zadatak2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Napišite program koji traži unos riječi sve dok se ne unese riječ "kraj".
                //Program ispisuje: riječi koje počinju slovom A, B, C, ostale riječi
                List<string> rijeci = new List<string>();
                while (true)
                {
                    Console.WriteLine("Upišite riječi: ");
                    string rijec = Console.ReadLine();
                    if (rijec.ToLower().Equals("kraj"))
                    {
                        break;
                    }
                    else
                    {
                        rijeci.Add(rijec);
                    }
                }
                List<string> rijeciKojePocinjuSaSlovomA = (  //WORDS START WITH LETTER "A"
                    from r in rijeci
                    where r.TrimStart().StartsWith("A")
                    select r).ToList();
                Console.WriteLine();
                Console.Write("Riječi koje počinju sa slovom \"A\" su: ");
                foreach (string r in rijeciKojePocinjuSaSlovomA)
                {
                    Console.Write(r + ",");
                }
                List<string> rijeciKojePocinjuSaSlovomB = ( //WORDS START WITH LETTER "B"
                     from r in rijeci
                     where r.TrimStart().StartsWith("B")
                     select r).ToList();
                Console.WriteLine();
                Console.Write("Riječi koje počinju sa slovom \"B\" su: ");
                foreach (string r in rijeciKojePocinjuSaSlovomB)
                {
                    Console.Write(r + ",");
                }
                List<string> rijeciKojePocinjuSaSlovomC = ( //WORDS START WITH LETTER "C"
                     from r in rijeci
                     where r.TrimStart().StartsWith("C")
                     select r).ToList();
                Console.WriteLine();
                Console.Write("Riječi koje počinju sa slovom \"C\" su: ");
                foreach (string r in rijeciKojePocinjuSaSlovomC)
                {
                    Console.Write(r + ",");
                }
                //List<string> ostaleRijeci = ( //WORD START WITH ANY OTHER LETTER BUT "A", "B" OR "C".
                //WORD START WITH ANY OTHER LETTER BUT "A", "B" OR "C".
                List<string> ostaleRijeci = ( //WORD START WITH ANY OTHER LETTER BUT "A", "B" OR "C".
                  from r in rijeci
                  where !r.TrimStart().StartsWith("A") && !r.TrimStart().StartsWith("B") && !r.TrimStart().StartsWith("C")
                  select r).ToList();
                Console.WriteLine();
                Console.Write("Ostale riječi: ");
                foreach (string r in ostaleRijeci)
                {
                    Console.Write(r + ",");
                }
                Console.ReadKey();
            }
        }
    }
    

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 19, 2018 3:35 AM
    Moderator