chore(CSharp): add character limit (#740)
Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>pull/746/head
parent
1790d56236
commit
09b5568315
|
@ -22,6 +22,7 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
|
|||
## Strings
|
||||
- [Palindrome](src/Strings/palindrome.cs)
|
||||
- [Trie](src/Strings/trie.cs)
|
||||
- [Character Limit](src/Strings/character-limit.cs)
|
||||
|
||||
## Search
|
||||
- [Binary Search](src/Search/binary-search.cs)
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
//Time Complexity: O(N)
|
||||
namespace Algorithms.Strings
|
||||
{
|
||||
public class TextHelper
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
Console.WriteLine("Please enter a string");
|
||||
string input = Console.ReadLine();
|
||||
|
||||
while (input == "")
|
||||
{
|
||||
Console.WriteLine("Please enter a string");
|
||||
input = Console.ReadLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("Please enter the number of characters you want to show");
|
||||
string input2 = Console.ReadLine();
|
||||
|
||||
while (!input2.All(char.IsDigit) || input2.All(char.IsWhiteSpace))
|
||||
{
|
||||
Console.WriteLine("The number you have entered is invalid. Please try again:");
|
||||
input2 = Console.ReadLine();
|
||||
}
|
||||
|
||||
int num = Convert.ToInt32(input2);
|
||||
Console.WriteLine(CharacterLimit(input, num));
|
||||
}
|
||||
|
||||
//Limits the the amount of characters shown in a string.
|
||||
public static string CharacterLimit(string input, int num)
|
||||
{
|
||||
//converts a string to a character array
|
||||
char[] ch = input.ToCharArray();
|
||||
|
||||
//loops through the array from the last element to the first, and replaces each element with a "."
|
||||
for (int i = ch.Count(); i > num; i--)
|
||||
{
|
||||
ch[i - 1] = '.';
|
||||
}
|
||||
|
||||
//converts character array back to string to be returned by the method
|
||||
string output = new string(ch);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
using NUnit.Framework;
|
||||
|
||||
namespace Algorithms.CSharp.Test.Strings
|
||||
{
|
||||
[TestFixture]
|
||||
internal class Character_Limit
|
||||
{
|
||||
[TestCase("Hello", "Hel..")]
|
||||
[TestCase("World", "Wor..")]
|
||||
[TestCase("How Ya", "How...")]
|
||||
[TestCase("Doin", "Doi.")]
|
||||
public void PassString_ShouldGetExpectedResult(string text, string expected)
|
||||
{
|
||||
string result = Algorithms.Strings.TextHelper.CharacterLimit(text, 3);
|
||||
Assert.AreEqual(expected, result);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue