https://www.csharpstar.com/csharp-program-to-calculate-factorial/
First Reverse
Description: For this challenge you will be reversing a string.
Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH.
Examples
Input: "coderbyte"
Output: etybredoc
using System;
class MainClass {
public static string FirstReverse(string str) {
string reverse = String.Empty;
for (int i = str.Length - 1; i >= 0; i--)
{
reverse += str[i];
}
return reverse;
}
static void Main() {
// keep this function call here
Console.WriteLine(FirstReverse(Console.ReadLine()));
}
}
Input: "I Love Code"
Output: edoC evoL I
First Factorial
Description: For this challenge you will be determining the factorial for a given number.
Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4,
then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.
using System;
class MainClass {
public static int FirstFactorial(int num) {
var result = 1;
for(var i = num; i > 1; i--){
result *= i;
}
return result;
}
static void Main() {
// keep this function call here
Console.WriteLine(FirstFactorial(Console.ReadLine()));
}
}
Input: 4
Output: 24
Longest Word
Description: For this challenge you will be determining the largest word in a string.
Have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string.
If there are two or more words that are the same length, return the first word from the string with that length.
Ignore punctuation and assume sen will not be empty.
using System;
using System.Linq;
class MainClass {
public static string LongestWord(string sen) {
string[] words = sen.Split(' ');
return words.OrderByDescending( s => s.Length ).First();;
}
static void Main() {
// keep this function call here
Console.WriteLine(LongestWord(Console.ReadLine()));
}
}
Input: "I love dogs"
Output: love
Simple Adding
Description: For this challenge you will be adding up all the numbers from 1 to a certain argument.
Have the function SimpleAdding(num) add up all the numbers from 1 to num.
For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10.
For the test cases, the parameter num will be any number from 1 to 1000.
using System;
class MainClass {
public static int SimpleAdding(int num) {
// code goes here
int start = 0;
for (int i = 1; i < num+1;i++){
start = start + i;
}
num = start;
return num;
}
static void Main() {
// keep this function call here
Console.WriteLine(SimpleAdding(Console.ReadLine()));
}
}
Input: 12
Output: 78
Letter Capitalize
Description: For this challenge you will be capitalizing certain characters in a string.
using System;
using System.Globalization;
class MainClass {
public static string LetterCapitalize(string str) {
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
return myTI.ToTitleCase(str);
}
static void Main() {
// keep this function call here
Console.WriteLine(LetterCapitalize(Console.ReadLine()));
}
}
Input: "hello world"
Output: Hello World
Word Count
Description: For this challenge you will be determining how many words a sentence contains.
Have the function WordCount(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces.
Examples
Input: "Hello World"
Output: 2
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static int WordCount(string str) {
var inputString = str.Split(' ');
int WordCount = 0;
foreach (var s in inputString)
{
WordCount++;
}
return WordCount;
}
static void Main() {
// keep this function call here
Console.WriteLine(WordCount(Console.ReadLine()));
}
}
Palindrome String
static void Main(string[] args)
{
string _inputstr, _reversestr = string.Empty;
Console.Write("Enter a string : ");
_inputstr = Console.ReadLine();
if (_inputstr != null)
{
for (int i = _inputstr.Length - 1; i >= 0; i--)
{
_reversestr += _inputstr[i].ToString();
}
if (_reversestr == _inputstr)
{
Console.WriteLine("String is Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
}
else
{
Console.WriteLine("String is not Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
}
}
Console.ReadLine();
}
Reverse Polish notation / Post-fix notation
using System;
using System.Collections;
using System.Text. RegularExpressions;
class MainClass
{
static void Main()
{
string result = Evaluate("2 12 + 7 /");
Console.WriteLine("Reverse Polish notation output : " + result);
Console.ReadLine();
}
public static string Evaluate(string postfixExpression)
{
Regex _operandRegex = new Regex(@"-?[0-9]+");
Regex _operatorRegex = new Regex(@"[+\-*\/]");
var tokens = new Stack();
string[] rawTokens = postfixExpression.Split(' ');
foreach (var t in rawTokens)
{
if (_operandRegex.IsMatch(t))
tokens.Push(t);
else if (_operatorRegex.IsMatch(t))
{
var t1 = tokens.Pop().ToString();
var t2 = tokens.Pop().ToString();
var op = t;
var result = EvaluateSingleExpression(t2, t1, op);
if (result != null)
tokens.Push(result);
}
}
if (tokens.Count > 0)
return tokens.Pop().ToString();
return "";
}
private static string EvaluateSingleExpression( string value1, string value2, string strOperator)
{
var operand1 = Convert.ToDouble(value1);
var operand2 = Convert.ToDouble(value2);
switch (strOperator)
{
case "+":
var plusRsult = operand1 + operand2;
return Convert.ToString(plusRsult);
case "-":
var minusRsult = operand1 - operand2;
return Convert.ToString(minusRsult);
case "/":
var divisionRsult = operand1 / operand2;
return Convert.ToString( divisionRsult);
case "*":
var multiplicationRsult = operand1 / operand2;
return Convert.ToString( multiplicationRsult);
default:
Console.WriteLine("An unexpected value");
break;
}
return null;
}
}
First Reverse
Description: For this challenge you will be reversing a string.
Have the function FirstReverse(str) take the str parameter being passed and return the string in reversed order. For example: if the input string is "Hello World and Coders" then your program should return the string sredoC dna dlroW olleH.
Examples
Input: "coderbyte"
Output: etybredoc
using System;
class MainClass {
public static string FirstReverse(string str) {
string reverse = String.Empty;
for (int i = str.Length - 1; i >= 0; i--)
{
reverse += str[i];
}
return reverse;
}
static void Main() {
// keep this function call here
Console.WriteLine(FirstReverse(Console.ReadLine()));
}
}
Input: "I Love Code"
Output: edoC evoL I
First Factorial
Description: For this challenge you will be determining the factorial for a given number.
Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4,
then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.
using System;
class MainClass {
public static int FirstFactorial(int num) {
var result = 1;
for(var i = num; i > 1; i--){
result *= i;
}
return result;
}
static void Main() {
// keep this function call here
Console.WriteLine(FirstFactorial(Console.ReadLine()));
}
}
Input: 4
Output: 24
Longest Word
Description: For this challenge you will be determining the largest word in a string.
Have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string.
If there are two or more words that are the same length, return the first word from the string with that length.
Ignore punctuation and assume sen will not be empty.
using System;
using System.Linq;
class MainClass {
public static string LongestWord(string sen) {
string[] words = sen.Split(' ');
return words.OrderByDescending( s => s.Length ).First();;
}
static void Main() {
// keep this function call here
Console.WriteLine(LongestWord(Console.ReadLine()));
}
}
Input: "I love dogs"
Output: love
Simple Adding
Description: For this challenge you will be adding up all the numbers from 1 to a certain argument.
Have the function SimpleAdding(num) add up all the numbers from 1 to num.
For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10.
For the test cases, the parameter num will be any number from 1 to 1000.
using System;
class MainClass {
public static int SimpleAdding(int num) {
// code goes here
int start = 0;
for (int i = 1; i < num+1;i++){
start = start + i;
}
num = start;
return num;
}
static void Main() {
// keep this function call here
Console.WriteLine(SimpleAdding(Console.ReadLine()));
}
}
Input: 12
Output: 78
Letter Capitalize
Description: For this challenge you will be capitalizing certain characters in a string.
using System;
using System.Globalization;
class MainClass {
public static string LetterCapitalize(string str) {
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
return myTI.ToTitleCase(str);
}
static void Main() {
// keep this function call here
Console.WriteLine(LetterCapitalize(Console.ReadLine()));
}
}
Input: "hello world"
Output: Hello World
Word Count
Description: For this challenge you will be determining how many words a sentence contains.
Have the function WordCount(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces.
Examples
Input: "Hello World"
Output: 2
using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
public static int WordCount(string str) {
var inputString = str.Split(' ');
int WordCount = 0;
foreach (var s in inputString)
{
WordCount++;
}
return WordCount;
}
static void Main() {
// keep this function call here
Console.WriteLine(WordCount(Console.ReadLine()));
}
}
Palindrome String
static void Main(string[] args)
{
string _inputstr, _reversestr = string.Empty;
Console.Write("Enter a string : ");
_inputstr = Console.ReadLine();
if (_inputstr != null)
{
for (int i = _inputstr.Length - 1; i >= 0; i--)
{
_reversestr += _inputstr[i].ToString();
}
if (_reversestr == _inputstr)
{
Console.WriteLine("String is Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
}
else
{
Console.WriteLine("String is not Palindrome Input = {0} and Output= {1}", _inputstr, _reversestr);
}
}
Console.ReadLine();
}
Reverse Polish notation / Post-fix notation
using System;
using System.Collections;
using System.Text.
class MainClass
{
static void Main()
{
string result = Evaluate("2 12 + 7 /");
Console.WriteLine("Reverse Polish notation output : " + result);
Console.ReadLine();
}
public static string Evaluate(string postfixExpression)
{
Regex _operandRegex = new Regex(@"-?[0-9]+");
Regex _operatorRegex = new Regex(@"[+\-*\/]");
var tokens = new Stack();
string[] rawTokens = postfixExpression.Split(' ');
foreach (var t in rawTokens)
{
if (_operandRegex.IsMatch(t))
tokens.Push(t);
else if (_operatorRegex.IsMatch(t))
{
var t1 = tokens.Pop().ToString();
var t2 = tokens.Pop().ToString();
var op = t;
var result = EvaluateSingleExpression(t2, t1, op);
if (result != null)
tokens.Push(result);
}
}
if (tokens.Count > 0)
return tokens.Pop().ToString();
return "";
}
private static string EvaluateSingleExpression(
{
var operand1 = Convert.ToDouble(value1);
var operand2 = Convert.ToDouble(value2);
switch (strOperator)
{
case "+":
var plusRsult = operand1 + operand2;
return Convert.ToString(plusRsult);
case "-":
var minusRsult = operand1 - operand2;
return Convert.ToString(minusRsult);
case "/":
var divisionRsult = operand1 / operand2;
return Convert.ToString(
case "*":
var multiplicationRsult = operand1 / operand2;
return Convert.ToString(
default:
Console.WriteLine("An unexpected value");
break;
}
return null;
}
}