Skip to content

Commit 430d541

Browse files
committed
Added class and test method for Codeland Username Validation code challenge.
1 parent 3408be4 commit 430d541

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

Coderbyte_CSharp/1 Easy Challenges/EasyChallenges.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ Remove Brackets 02/24/2021 StringBrackets
2222
Simple Adding 02/24/2021 MathSummation
2323
Simple Symbols 02/24/2021 StringSymbols
2424
String Periods 02/24/2021 StringPeriod
25-
Time Convert 2/10/2021 TimeConverter
26-
Username Validation
27-
Vowel Square 2/23/2021 SquareVowels
25+
Time Convert 02/10/2021 TimeConverter
26+
Username Validation 02/24/2021 UsernameValidation
27+
Vowel Square 02/23/2021 SquareVowels
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
3+
namespace Coderbyte_CSharp._1_Easy_Challenges
4+
{
5+
class UsernameValidation
6+
{
7+
//Have the function CodelandUsernameValidation(str) take the str parameter being
8+
// passed and determine if the string is a valid username according to the
9+
//following rules :
10+
11+
//1. The username is between 4 and 25 characters.
12+
//2. It must start with a letter.
13+
//3. It can only contain letters, numbers, and the underscore character.
14+
//4. It cannot end with an underscore character.
15+
16+
//If the username is valid then your program should return the string true,
17+
//otherwise return the string false.
18+
19+
public string CodelandUsernameValidation(string str)
20+
{
21+
string result = String.Empty;
22+
23+
bool rule1 = ValidateLength(str);
24+
bool rule2 = ValidateStartWithLetter(str);
25+
bool rule3 = ValidateOnlyValidCharacters(str);
26+
bool rule4 = ValidateEndCharacter(str);
27+
28+
result = (rule1 && rule2 && rule3 && rule4) ? "true" : "false";
29+
30+
31+
return result;
32+
}
33+
34+
protected bool ValidateLength(string str)
35+
{
36+
bool isValid = false;
37+
38+
int length = str.Length;
39+
40+
isValid = (length >= 4 && length <= 25);
41+
42+
return isValid;
43+
}
44+
45+
protected bool ValidateStartWithLetter(string str)
46+
{
47+
bool isValid = false;
48+
49+
isValid = Char.IsLetter(str[0]);
50+
51+
return isValid;
52+
}
53+
54+
protected bool ValidateOnlyValidCharacters(string str)
55+
{
56+
bool isValid = true;
57+
58+
foreach (char c in str)
59+
{
60+
if (!Char.IsLetterOrDigit(c) && c != '_')
61+
{
62+
isValid = false;
63+
break;
64+
}
65+
}
66+
67+
return isValid;
68+
}
69+
70+
protected bool ValidateEndCharacter(string str)
71+
{
72+
bool isValid = false;
73+
int length = str.Length;
74+
75+
isValid = (str[length - 1] != '_');
76+
77+
return isValid;
78+
}
79+
80+
81+
}
82+
}

Coderbyte_CSharp/Coderbyte_CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="1 Easy Challenges\StringSymbols.cs" />
6464
<Compile Include="1 Easy Challenges\StringWords.cs" />
6565
<Compile Include="1 Easy Challenges\TimeConverter.cs" />
66+
<Compile Include="1 Easy Challenges\UsernameValidation.cs" />
6667
<Compile Include="2 Medium Challenges\ConsecutiveNumbers.cs" />
6768
<Compile Include="2 Medium Challenges\NumberEncoder.cs" />
6869
<Compile Include="2 Medium Challenges\PrimeNumber.cs" />

Coderbyte_CSharp/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static void Main(string[] args)
3636
tc.Test_SimpleAdding();
3737
tc.Test_SimpleSymbols();
3838
tc.Test_StringPeriods();
39+
tc.Test_UsernameValidation();
3940

4041
// Medium
4142
Console.WriteLine("Medium Code Challenges:");

Coderbyte_CSharp/TestChallenge.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using Coderbyte_CSharp._1_Easy_Challenges;
56
using Coderbyte_CSharp.Easy_Challenges;
67
using Coderbyte_CSharp.Medium_Challenges;
78
using Coderbyte_CSharp.Hard_Challenges;
@@ -418,6 +419,28 @@ public void Test_StringPeriods()
418419
Console.WriteLine();
419420
}
420421

422+
public void Test_UsernameValidation()
423+
{
424+
UsernameValidation validation = new UsernameValidation();
425+
Console.WriteLine("Username Validation:");
426+
427+
string str = "jgklfnklg_";
428+
Console.WriteLine("Input: {0}", str);
429+
Console.WriteLine("Output: {0}", validation.CodelandUsernameValidation(str));
430+
Console.WriteLine();
431+
432+
str = "quick_gamester";
433+
Console.WriteLine("Input: {0}", str);
434+
Console.WriteLine("Output: {0}", validation.CodelandUsernameValidation(str));
435+
Console.WriteLine();
436+
437+
str = "jkfld%jfkdsl";
438+
Console.WriteLine("Input: {0}", str);
439+
Console.WriteLine("Output: {0}", validation.CodelandUsernameValidation(str));
440+
Console.WriteLine();
441+
442+
}
443+
421444

422445
#endregion
423446

0 commit comments

Comments
 (0)