Q. C# Program to find length of a given string.


Here you will find an algorithm and program in C# programming language to find length of given string. First let us understand string in C#.

Explanation : The string is defined as an array of characters. The difference between a character array and a string is the string ends with a special character '\0'.

For Example : If the string is "hello", then the output will be 5.

Algorithm to find length of string


START
Step 1: Length = 0
Step 2: Repeat step 3 foreach char chr in str
Step 3: Length = Length + 1
Step 4: Return Length
STOP

C# Program to Find the Length of a String

using System;  
public class LFC
{  
	public static void Main() 
	{
		string str1="Letsfindcourse"; /* Declares a string of size 100 */
		int l= 0;
		foreach(char chr in str1)
		{
			l += 1;
		}
		Console.Write("Length of the string = {0}\n\n", l);
	}
}

Output

Length of the string = 14