// 统计字符串中数字,大小写以及空格的个数。
#define maxsize 100
#include <stdio.h>
#include <ctype.h>
int main()
{
int dxnum=0,xxnum=0,spnum=0,digitnum=0;
int i=0;
char *str;
str=(char *)malloc(maxsize*sizeof(char));
if(str==NULL)
{
printf("malloc failure!/n");
exit(-1);
}
clrscr();
printf("input a sentence!/n");
fgets(str,maxsize,stdin);
while(str[i]!='/0')
{
if(islower(str[i]))
xxnum++;
if(isupper(str[i]))
dxnum++;
if(str[i]==' ')
spnum++;
if(isdigit(str[i]))
digitnum++;
i++;
}
printf("*************the number list***********/n");
printf("Dxnum=%d,Xxnum=%d,SPnum=%d,Digitalnum=%d/n",dxnum,xxnum,spnum,digitnum);
}
//计算字符串中单词和字符的个数
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define maxsize 100
int main(void)
{
int i=0,flag=0;
int ch_num=0;
int count=0;
char *buff;
clrscr();
buff=(char *)malloc(maxsize*sizeof(char));
if(buff==NULL)
{
printf("MALLOC failure!/n");
exit(-1);
}
printf("input sentences!:");
fgets(buff,maxsize,stdin);
while(buff[i]!='/0'&&buff[i]!='/n')
{
if(islower(buff[i])||isupper(buff[i]))
{ if(!flag)
count++;
else ;
flag=1;
}
else
{
flag=0;
ch_num++;
}
i++; /* next word*/
}
printf("***********************/n");
printf("there is %d words in %s/n",count,buff);
printf("string_lenth is %d,and character_lenth is %d/n",strlen(buff),ch_num);
}
程序运行结果: