判断一个字符串是否是首字母大写且非首字母小写。 c语言编程

如果输入字符串首字符为大写字母且其他字符非大写字母,返回true
其他情况(空字符串、首字符非字母、首字母为小写、首字母大写但其他字符非字母等)均返回false
个任意字符串,长度不超过128个字符
判断一个字符串是否是首字母大写且非首字母小写。

    可写一个子函数来进行判断,首先判断首字母是否为小写字母,如果是,则不满足条件,函数返回0.之后,循环判断后续字母,若其为大写字母,则函数返回0.最后,若函数没有返回,则说明字符串满足条件,函数返回1.

代码如下:

#include <string.h>

int check(char *str)
{
int i;

if (str[0] < 'A' || str[0] > 'Z')
return 0;
for (i = 1; i < strlen(str); i++)
if (str[i] >= 'A' && str[i] <= 'Z')
return 0;

return 1;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-06
# define TRUE 1
# define FALSE 0
check(str)
char * str;
{
int i,result;
int len;
len=strlen(str);
result=FALSE;
if(str[0]>='A' && str[0]<='Z')
{
result=TRUE;
for(i=1;i<len;i++)
{
if(str[i]<'a' || str[i]>'z')
{
result=FALSE;
break;
}
}
}
printf("str=%s result=%d\n",str,result);
return result;
}本回答被提问者和网友采纳
第2个回答  2013-10-09
利用ASCII码值作为界限,大小写字母值不一样的

#include <stdio.h>
# define true 1
# define false 0
int main()
{
char str[];
get(str);
getchar();
int i=0;
if (str[0]>90&&str[0]<65)
return false;
else
while(str[i]!='/0')
{i++;
if ((str[0]<=90&&str[0]>=65)
return false;
}
return true;
}
第3个回答  2013-10-09
#include <ctype.h>

#define TRUE 1
#define FALSE 0

int isFirstUpper(char * s)
{
  if (*s == '\0') return FALSE;
  if (!isupper(*s)) return FALSE;
  while (*++s != '\0')
  {
    if (!islower(*s)) return FALSE;
  }
  return TRUE;
}

相似回答