C# Exercises

In order to get used to C#, the most important thing is practice. In this post I present 5 examples of simple exercises with strings. Try to solve them by yourself and if you miss something, you have also one possible solution in the code below.

For each string in an array of strings:

1) Create a function to calculate the reverse string. Ex: the reverse string of “rac” is “car”.
2) Calculate the number of vowels in the string.
3) Check if the string is palindrome (it can be read the same forwards as backwards. Ex: “eye”)
4) Calculate the string’s number of words.
5) Save on the current user’s desktop the results in a *.txt file named “Results”.

Results:

A) Console:

results image console.jpg

B) *.txt file:

Results

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace simpleStringsExercices
{
 class Program
 {
 static string reverseString(string s) //a function to reverse the string
 {
 int slen = s.Length;
 StringBuilder sb = new StringBuilder();

 for (int i = slen-1; i >= 0; i--)
 {
 sb.Append(s[i]);
 }
 return sb.ToString();
 }

 static int countvowels(string s) //a function to count vowels of string
 {
 int counter = 0;
 List<char> vowels = new List<char> { 'a', 'e', 'i', 'o', 'u' };

 foreach (char item in s.ToLower())
 {
 if (vowels.Contains(item))
 counter++;
 }
 return counter;
 }

 static bool check_palindrome(string s) //a function to check if the string is palindrome
 {
 string reversed = reverseString(s.ToLower());

 if (s.ToLower().Equals(reversed))
 return true;
 return false;
 }

 static int words_no(string s) //a function to count the number of words from the string
 {
 int counter = 0;
 string[] words = System.Text.RegularExpressions.Regex.Split(s, @"W+");
 foreach (string word in words)
 {
 counter++;
 }
 return counter;
 }

 static void saveToFile(List<string> lines) //a function to save to desktop in a txt file the items from a generic list
 {
 string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"//Results.txt";
 System.IO.File.WriteAllLines(path, lines);
 }


 static void Main(string[] args)
 {
 string[] strings = { "MADAM", "remmargorp a era uoY", "rotor", "learn c sharp" }; //our initial array with strings to check 
 List<string> lines = new List<string>(); //instantiate a generic list to add the lines to be written to txt file

 foreach (string input in strings)
 {
 
 //write to console
 Console.WriteLine("Initial string was: {0} nReversed string is: {1} nNo of vowels in the string is: {2} nIs palindrome: {3} nNo of words: {4} n ",
 input, reverseString(input), countvowels(input), check_palindrome(input), words_no(input));

 //add results to list
 lines.Add("Initial string was: " + input);
 lines.Add("Reversed string is: " + reverseString(input));
 lines.Add("No of vowels in the string is: " + countvowels(input));
 lines.Add("Is palindrome: " + check_palindrome(input));
 lines.Add("No of words: " + words_no(input));
 lines.Add("");

 }

 //call the save function to save the list with result to the *.txt file
 saveToFile(lines);

 
 }
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment