Tag Archives: alphabet position
C++ || How To Replace A Letter With Its Alphabet Position Using C++
The following is a module with functions which demonstrates how to replace a letter with its alphabet position using C++.
1. Replace With Alphabet Position
The example below demonstrates the use of ‘Utils::getAlphabetPosition‘ to replace a letter with its alphabet position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Replace With Alphabet Position // Get alphabet position auto result = Utils::getAlphabetPosition("The sunset sets at twelve o' clock."); // Display the results for (const auto& position : result) { std::cout << position << " "; } // expected output: /* 20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11 */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jun 3, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.h // Description: Handles general utility functions // ============================================================================ #pragma once #include <cctype> #include <vector> #include <string> namespace Utils { /** * FUNCTION: getAlphabetPosition * USE: Gets the alphabet position of each character in a string * @param text: The text to get the position * @return: The alphabet position of each character */ std::vector<std::size_t> getAlphabetPosition(std::string text) { std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; std::vector<std::size_t> result; for (const auto& letter : text) { auto index = alphabet.find(static_cast<unsigned char>(std::tolower(letter))); if (index != std::string::npos) { result.push_back(index + 1); } } return result; } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jun 3, 2021 // Taken From: http://programmingnotes.org/ // File: program.cpp // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ #include <iostream> #include <string> #include <exception> #include "Utils.h" void display(const std::string& message); int main() { try { // Get alphabet position auto result = Utils::getAlphabetPosition("The sunset sets at twelve o' clock."); // Display the results for (const auto& position : result) { std::cout << position << " "; } } catch (std::exception& e) { display("\nAn error occurred: " + std::string(e.what())); } std::cin.get(); return 0; } void display(const std::string& message) { std::cout << message << std::endl; }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
C# || How To Replace A Letter With Its Alphabet Position Using C#
The following is a module with functions which demonstrates how to replace a letter with its alphabet position using C#.
1. Replace With Alphabet Position
The example below demonstrates the use of ‘Utils.Methods.GetAlphabetPosition‘ to replace a letter with its alphabet position.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Replace With Alphabet Position // Get alphabet position var result = Utils.Methods.GetAlphabetPosition("The sunset sets at twelve o' clock."); // Display the results Console.WriteLine(string.Join(" ", result)); // expected output: /* 20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11 */ |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 14, 2021 // Taken From: http://programmingnotes.org/ // File: Utils.cs // Description: Handles general utility functions // ============================================================================ using System; using System.Collections.Generic; namespace Utils { public static class Methods { /// <summary> /// Gets the alphabet position of each character in a string /// </summary> /// <param name="text">The text to get the position</param> /// <returns>The alphabet position of each character</returns> public static List<int> GetAlphabetPosition(string text) { var alphabet = "abcdefghijklmnopqrstuvwxyz"; var result = new List<int>(); foreach (var letter in text.ToLower()) { var index = alphabet.IndexOf(letter); if (index > -1) { result.Add(index + 1); } } return result; } } }// http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// ============================================================================ // Author: Kenneth Perkins // Date: May 14, 2021 // Taken From: http://programmingnotes.org/ // File: Program.cs // Description: The following demonstrates the use of the Utils Namespace // ============================================================================ using System; using System.Diagnostics; using System.Collections.Generic; public class Program { static void Main(string[] args) { try { var result = Utils.Methods.GetAlphabetPosition("The sunset sets at twelve o' clock."); Display(string.Join(" ", result)); } catch (Exception ex) { Display(ex.ToString()); } finally { Console.ReadLine(); } } static void Display(string message) { Console.WriteLine(message); Debug.Print(message); } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Replace A Letter With Its Alphabet Position Using VB.NET
The following is a module with functions which demonstrates how to replace a letter with its alphabet position using VB.NET.
1. Replace With Alphabet Position
The example below demonstrates the use of ‘Utils.GetAlphabetPosition‘ to replace a letter with its alphabet position.
1 2 3 4 5 6 7 8 9 10 |
' Replace With Alphabet Position ' Get alphabet position Dim result = Utils.GetAlphabetPosition("The sunset sets at twelve o' clock.") ' Display the results Debug.Print(String.Join(" ", result)) ' expected output: ' 20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11 |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 30, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Gets the alphabet position of each character in a string ''' </summary> ''' <param name="text">The text to get the position</param> ''' <returns>The alphabet position of each character</returns> Public Function GetAlphabetPosition(text As String) As List(Of Integer) Dim alphabet = "abcdefghijklmnopqrstuvwxyz" Dim result = New List(Of Integer) For Each letter In text.ToLower Dim index = alphabet.IndexOf(letter) If index > -1 Then result.Add(index + 1) End If Next Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 30, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try Dim result = Utils.GetAlphabetPosition("The sunset sets at twelve o' clock.") Display(String.Join(" ", result)) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.