w3resource

C# Sharp Exercises: Alphabet position in a string

C# Sharp String: Exercise-62 with Solution

Write a C# Sharp program that takes a string and display all the characters with their respective numbers.

Sample Data:
("C Sharp") -> " 3 19 8 1 18 16"
("The quick brown fox jumps over the lazy dog.") -> " 20 8 5 17 21 9 3 11 2 18 15 23 14 6 15 24
10 21 13 16 19 15 22 5 18 20 8 5 12 1 26 25 4 15 7"
("HTML Tutorial") -> " 8 20 13 12 20 21 20 15 18 9 1 12 "

Sample Solution-1:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "The quick brown fox jumps over the lazy dog.";
            Console.WriteLine("Original string: " +text);
            Console.WriteLine("Alphabet position in the said string: " + test(text));
            text = "C Sharp";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Alphabet position in the said string: " + test(text));
            text = "HTML Tutorial";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Alphabet position in the said string: " + test(text));
        }  
        public static string test(string text)
        {
           var char_positions = text.ToLower().Where(ch => Char.IsLetter(ch)).
           Select(ch => (int)ch % 32).ToArray();
           return string.Join(" ", char_positions);
        }
    }
}

Sample Output:

Original string: The quick brown fox jumps over the lazy dog.
Alphabet position in the said string: 20 8 5 17 21 9 3 11 2 18 15 23 14 6 15 24 10 21 13 16 19 15 22 5 18 20 8 5 12 1 26 25 4 15 7

Original string: C Sharp
Alphabet position in the said string: 3 19 8 1 18 16

Original string: HTML Tutorial
Alphabet position in the said string: 8 20 13 12 20 21 20 15 18 9 1 12

Flowchart :

Flowchart: C# Sharp Exercises - Alphabet position in a string.

Sample Solution-2:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "The quick brown fox jumps over the lazy dog.";
            Console.WriteLine("Original string: " +text);
            Console.WriteLine("Alphabet position in the said string: " + test(text));
            text = "C Sharp";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Alphabet position in the said string: " + test(text));
            text = "HTML Tutorial";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Alphabet position in the said string: " + test(text));
        }  
        public static string test(string text)
        {
           return String.Join(" ", text.Where(ch => char.IsLetter(ch)).Select(ch => (int)ch % 32));
        }
    }
}

Sample Output:

Original string: The quick brown fox jumps over the lazy dog.
Alphabet position in the said string: 20 8 5 17 21 9 3 11 2 18 15 23 14 6 15 24 10 21 13 16 19 15 22 5 18 20 8 5 12 1 26 25 4 15 7

Original string: C Sharp
Alphabet position in the said string: 3 19 8 1 18 16

Original string: HTML Tutorial
Alphabet position in the said string: 8 20 13 12 20 21 20 15 18 9 1 12

Flowchart :

Flowchart: C# Sharp Exercises - Alphabet position in a string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Count Occurrences of a substring in a string.
Next C# Sharp Exercise: Number of times a substring appeared in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter