33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool detect_capital_use (char *word);
bool check_word(char *word);
int main(void)
{
char word[32];
while (1) {
printf("Please input a word: ");
scanf("%s", word);
if (!check_word(word)) {
printf("Not a word!\n");
continue;
}
if (strcmp(word, "exit") == 0)
break;
if (detect_capital_use(word))
printf("the word of %s's captial is right used!\n", word);
else
printf("the word of %s's captial is not right used!\n", word);
}
return 0;
}
bool check_word(char *word)
{
int i;
bool flag = true;
int len = strlen(word);
for (i = 0; i < len; i++)
if (! ((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z'))) {
break;
}
if (i < len)
flag = false;
return flag;
}
bool detect_capital_use (char *word)
{
bool flag = true;
int i;
int len = strlen(word);
if (word[0] >= 'A' && word[0] <= 'Z') {
if (word[1] >= 'A' && word[1] <= 'Z') {
for (i = 2; i < len; i++) {
if (word[i] >= 'a' && word[i] <= 'z') {
flag = false;
break;
}
}
}
} else if (word[0] >= 'a' && word[0] <= 'z') {
for (i = 1; i < len; i++)
if (word[i] >= 'A' && word[i] <= 'Z') {
flag = false;
break;
}
} else {
flag = false;
}
return flag;
}