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:
parent
ef084b6bfe
commit
43fe49a5be
3
challenges/acronyms/solutions/cs/with-loops/README.md
Normal file
3
challenges/acronyms/solutions/cs/with-loops/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# acronyms/cs/with-loops
|
||||||
|
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) on 10 September 2021.
|
82
challenges/acronyms/solutions/cs/with-loops/Solution.cs
Normal file
82
challenges/acronyms/solutions/cs/with-loops/Solution.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user