-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeland_Username_Validation.c
74 lines (58 loc) · 1.87 KB
/
Codeland_Username_Validation.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*Codeland Username Validation
Have the function CodelandUsernameValidation(str) take the str parameter being passed
and determine if the string is a valid username according to the following rules:
1. The username is between 4 and 25 characters.
2. It must start with a letter.
3. It can only contain letters, numbers, and the underscore character.
4. It cannot end with an underscore character.
If the username is valid then your program should return the string true, otherwise
return the string false.
https://coderbyte.com/editor/Codeland%20Username%20Validation:C */
#include <stdio.h>
#include <stdalign.h>
#include <string.h>
char * CodelandUsernameValidation(char User[]){
User[strcspn(User, "\n")] = '\0';
int flag = 0;
// Check if the 'User' has between 4 and 25 characters
if (!(strlen(User) >= 4 && strlen(User) <= 25))
{
flag++;
}
// Checks if the first character of 'User' is a letter
if (!((User[0] >= 'a' && User[0] <= 'z') || (User[0] >= 'A' && User[0] <= 'Z')))
{
flag++;
}
// Checks if the user is made up of only letters, numbers and the underscore character
for (int i = 0; User[i] != '\0'; i++)
{
if (!((User[i] >= 65 && User[i] <= 90) || (User[i] >= 97 && User[i] <= 122) ||
(User[i] >= 48 && User[i] <= 57) || (User[i] == 95)))
{
flag++;
}
}
//Checks if the last character of User ends with the underscore character
if (User[strlen(User) - 1] == 95)
{
flag++;
}
//If successful, the flag variable is not incremented and then 'true' is returned.
if (flag == 0)
{
return "true";
}
else
{
return "false";
}
}
int main()
{
char UserName[30];
printf("User name: ");
fgets(UserName, 30, stdin);
printf("Status: %s.\n", CodelandUsernameValidation(UserName));
return 0;
}