1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(solutions): add acronyms/cs/with-loops

This commit is contained in:
Divlo 2021-09-10 22:18:41 +02:00
parent ef084b6bfe
commit 43fe49a5be
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# acronyms/cs/with-loops
Created by [@Divlo](https://github.com/Divlo) on 10 September 2021.

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
namespace Solution
{
class Program
{
static void Main()
{
string line = Console.ReadLine();
Console.WriteLine(GetAcronym(line));
}
/// <summary>
/// Returns the acronym of the given string.
/// </summary>
/// <param name="line">The string to get the acronym of.</param>
/// <returns>The acronym of the given string.</returns>
public static string GetAcronym(string line)
{
line = ReplaceString(line, "\"", "");
string[] words = SplitString(line, " ");
string acronym = "";
foreach (string word in words)
{
acronym += word[0].ToString().ToUpper();
}
return acronym;
}
/// <summary>
/// Splits a string into words.
/// </summary>
/// <param name="source">The string to split.</param>
/// <param name="delimiter">The delimiter to split the string by.</param>
/// <returns>An array of words.</returns>
public static string[] SplitString(string source, string delimiter)
{
List<string> result = new List<string>();
string current = "";
for (int index = 0; index < source.Length; index++)
{
string character = source[index].ToString();
if (character == delimiter)
{
result.Add(current);
current = "";
}
else
{
current += character;
}
}
result.Add(current);
return result.ToArray();
}
/// <summary>
/// Replaces all instances of a string with another string.
/// </summary>
/// <param name="source">The source string.</param>
/// <param name="oldValue">The string to replace.</param>
/// <param name="newValue">The string to replace with.</param>
/// <returns>The new string.</returns>
public static string ReplaceString(string source, string oldValue, string newValue)
{
string result = "";
for (int index = 0; index < source.Length; index++)
{
if (source[index].ToString() == oldValue)
{
result += newValue;
}
else
{
result += source[index];
}
}
return result;
}
}
}