Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Find the length of a string

C# Sharp String: Exercise-2 with Solution

Write a program in C# Sharp to find the length of a string without using library function.

C# Sharp Exercises: Find the length of a string

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise2
{  
    public static void Main() 
{
    string str; /* Declares a string of size 100 */
    int l= 0;
	
      Console.Write("\n\nFind the length of a string :\n");
      Console.Write("---------------------------------\n"); 	
      Console.Write("Input the string : ");
      str = Console.ReadLine();

         foreach(char chr in str)
        {
            l += 1;

        }

   Console.Write("Length of the string is : {0}\n\n", l);
	}
}

Sample Output:

Find the length of a string :                                          
---------------------------------                                      
Input the string : w3resource                                          
Length of the string is : 10 

Flowchart:

Flowchart: Find the length of a string

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to input a string and print it.
Next: Write a program in C# Sharp to separate the individual characters from a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.