This is a C# Program to count lines in a string.
Problem Description
This C# Program Counts Lines in a String.
Problem Solution
Here the Number of Lines in the given string is calculated and are stored in count variable and are displayed.
Program/Source Code
Here is source code of the C# Program to Count Lines in a String. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
/* * C# Program to Count Lines in a String */ 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; } }
Program Explanation
This C# program is used to count lines in a string. Using while loop count the lines in string. Check the index of sentence is equal to -1. If the condition is true, then execute the statement and print the number of lines in the string.
advertisement
advertisement
Runtime Test Cases
Number of Lines in the String : 3
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
If you wish to look at all C# Programming examples, go to 1000 C# Programs.
Related Posts:
- Practice MCA MCQs
- Apply for Computer Science Internship
- Check Computer Science Books
- Check C# Books
- Apply for C# Internship