C# Sharp Exercises: Check the length of a given string is odd or even
C# Sharp Basic: Exercise-74 with Solution
Write a C# Sharp program to check the length of a given string is odd or even. Return 'Odd length' if the string length is odd otherwise 'Even length'.
Sample Solution:
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Original string: PHP");
Console.WriteLine("Convert the letters of the said string into alphabetical order: " + test("PHP"));
Console.WriteLine("Original string: javascript");
Console.WriteLine("Convert the letters of the said string into alphabetical order: " + test("javascript"));
Console.WriteLine("Original string: python");
Console.WriteLine("Convert the letters of the said string into alphabetical order: " + test("python"));
}
public static string test(string word)
{
int length = word.Length;
if (length % 2 == 0)
{
return "Even length";
}
else
{
return "Odd length";
}
}
}
}
Sample Output:
Original string: PHP Convert the letters of the said string into alphabetical order: Odd length Original string: javascript Convert the letters of the said string into alphabetical order: Even length Original string: python Convert the letters of the said string into alphabetical order: Even length
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to convert the letters of a given string (same case-upper/lower) into alphabetical order.
Next: Write a C# Sharp program which takes a positive number and return the nth odd number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework