Saturday 2 December 2017

C# Program to Count Lines in a String


Code:

using System;
using System.Text.RegularExpressions;
class Program
{
    static void Main()
    {
        long a = countstring("This is \n Sanfoundry\n Website");
        Console.WriteLine("Number of Lines in the String : {0}",a);
        Console.ReadLine();
    }
    static long countstring(string s)
    {
        long count = 1;
        int start = 0;
        while ((start = s.IndexOf('\n', start)) != -1)
        {
            count++;
            start++;
        }
        return count;
    }
}


Output:

Number of Lines in the String : 3



More C# Programs:




















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...