Ilość znaków w wierszu zmiennej typu string

0

Program ma wyświetlić ilość wierszy oraz ilość znaków w każdym wierszu.

  class Program
    {
        static void Main(string[] args)
        {
            string tekst = "Now, fair Hippolyta, our nuptial hour\n" +
                            "Draws on apace; four happy days bring in\n" +
                            "Another moon: but, O, methinks, how slow\n" +
                            "This old moon wanes! she lingers my desires,\n" +
                            "Like to a step-dame or a dowager\n" +
                            "Long withering out a young man revenue.";

            int verseCounter = 0;
            int letterCounter = 0;
            int position = 0;

            while ((position = tekst.IndexOf('\n', position + 1)) >= 0)
            {
                verseCounter++;

            }

            foreach (char letter in tekst)
            {
                if (letter != ' ')
                {
                    letterCounter++;
                }
            }

                Console.Write(tekst + "\n" + "tekst ma {0} ", verseCounter+1 + " linii\n");
                Console.WriteLine("Ilość znaków : {0}",letterCounter);

Wiersze liczy i na tą chwile zrobiłem tylko, żeby zliczał wszystkie znaki. Jak można to najprościej przerobić, żeby liczył znaki w wierszu.

1

Podziel całość na wiersze do tablicy czy listy i zlicz liczbę elementów, poszukaj pod kątem c# explode.
Btw, to że coś nie jest spacją, nie oznacza, że staje się literą.

1

(potrzebne System.Linq)

Func<char, bool> predicate = c => !char.IsWhiteSpace(c);
tekst.Split('\n').Select((line, idx) => string.Format("line {0} chars: {1}", idx+1, line.Count(predicate))).ToList().ForEach(Console.WriteLine);
Console.WriteLine("lines: {0} chars: {1}", tekst.Count(c => c == '\n')+1, tekst.Count(predicate));
0

Lece z zadaniami i materiałem tak jak w jednym podręczniku do C# dla newbie i na tą chwile LINQa nie miałem tam i nawet Split'a.
Doczytałem przed chwilą właśnie o split np. string[] lines = text.Split('\n'); i lines.Length zwróci mi ilość wierszy?. Więc teraz żeby zliczyć znaki w każdym wierszu muszę przelecieć po lines[i]..?

0

Tak, ale nic nie zaszkodzi jak wyprzedzisz materiał w książce i poczytasz o LINQ :P

1

bez linq:

		var lines = tekst.Split('\n');
		var totalChars = 0;
		for(var lineIdx=0;lineIdx<lines.Length;lineIdx++)
		{
			var lineChars = 0;
			foreach(var c in lines[lineIdx])
			{
				lineChars += char.IsWhiteSpace(c) ? 0 : 1;
			}
			Console.WriteLine("line {0}, chars: {1}", lineIdx+1, lineChars);
			totalChars += lineChars;
		}
		Console.WriteLine("total lines: {0}, chars: {1}", lines.Length, totalChars);
0

Ja bym to jeszcze lekko zmodyfikował, żeby było krócej (bez linq)

            tekst = tekst.Replace(" ", "");
            var lines = tekst.Split('\n');
            var totalChars = 0;
            var count = 1;
            foreach (string line in lines)
            {
                Console.WriteLine("line {0}, chars: {1}", count++ , line.Length);
                totalChars += line.Length;
            }
            Console.WriteLine("total lines: {0}, chars: {1}", lines.Length, totalChars);

1 użytkowników online, w tym zalogowanych: 0, gości: 1