-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy path520.c
43 lines (42 loc) · 897 Bytes
/
520.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
bool detectCapitalUse(char *word)
{
int len = strlen(word);
if (len == 1)
return 1;
int countUpper = 0, i;
for (i = 0; i < len; i++)
{
if (isupper(word[i]))
countUpper++;
}
/* All lower case */
if (countUpper == 0)
return 1;
/* 1st character is upper, and the rest is lower case */
if (countUpper == 1 && isupper(word[0]))
return 1;
/* Check all character is upper case? */
else
return countUpper == len;
}
/* Another way */
bool isAllUpper(char *word)
{
int len = strlen(word);
for (int i = 0; i < len; i++)
{
if (islower(word[i]))
return 0;
}
return 1;
}
bool detectCapitalUse(char *word)
{
int len = strlen(word);
for (int i = 1; i < len; i++)
{
if (isupper(word[i]) && !isAllUpper(word))
return 0;
}
return 1;
}