String program #1 (Length of string)
Enter a string: liveviar
Length of string: 8
The program for the string program is
written in C# programming language and will accept a string as input. The loops
will iterate based on the string of entry.
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
namespace patternProblem.String
{
class String1 //find length of string
{
static void Main(string[] args)
{
Console.Write("Enter a string: ");
string str = Console.ReadLine();
int
count = 0;
//Method-1 (Using in-built length property)
Console.WriteLine("Length of string: {0}", str.Length);
//Method-2 (Using foreach loop)
foreach (char c in str)
{
count++;
}
Console.WriteLine("Length of string: {0}", count);
Console.ReadKey();
}
}
}
Output:
The input string
here is liveviar. So, the length of string is 8. The output is shown below:
Enter a string: liveviar
Length of string: 8
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table String based program
Keep learning and sharing...
No comments:
Post a Comment