当前位置:编程学习 > C/C++ >>

一个统计输入字符串中各种字符个数的C语言代码,求指点。。

//输入一行文字,找出其中大写字母,小写字母,空格,数字以及其它字符各有多少。

#include "stdafx.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
 void star_str(int *p1,int *p2,int *p3,int *p4,int *p5,char *p);
 char string[50];
 char *p;
 int cap,low,blan,num,other,i;
 int *p1=&cap,*p2=&low,*p3=&blan,*p4=&num,*p5=&other;
 printf("请输入字符串:\n");
 for(i=0;string[i]=getchar()!='\n';i++)//这里不知道怎么输写
 {
 }
 p=string;
 puts(p);
 star_str(p1,p2,p3,p4,p5,p);
 printf("有%d个大写字母,%d个小写字母,%d个空格,%d个数字,%d个其它字符",*p1,*p2,*p3,*p4,*p5);
    return 0;
}
void star_str(int *p1,int *p2,int *p3,int *p4,int *p5,char *p)
{
 for(;*p!='\n';p++)//这里不怎么怎么写,循环条件怎么确定?
 {
     if('A'<=*p<='Z')
      (*p1)++;
     else
      if('a'<=*p<='z')
       (*p2)++;
      else
       if(*p=' ')
        (*p3)++;
       else
           if(0<=*p<=9)
         (*p4)++;
        else
         (*p5)++;
 }
}

追问:嗯。。。小弟不急。。。
答案:
稍等 

如此即可:

 

// test_count.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include "string.h"
#include "ctype.h"

int main(int argc, char* argv[])
{
 void star_str(int *p1,int *p2,int *p3,int *p4,int *p5,char *p);
 char string[50];
 char *p = string;
 int cap=0;
 int low=0;
 int blan=0;
 int num=0;
 int other=0;
 int i=0;
 int *p1=&cap,*p2=&low,*p3=&blan,*p4=&num,*p5=&other;
 printf("??ê?è?×?·?′?:\n");
 gets(p);

 puts(p);
 star_str(p1,p2,p3,p4,p5,p);
 printf("óD%d??′óD′×???£?%d??D?D′×???£?%d??????£?%d??êy×?£?%d?????ü×?·?",*p1,*p2,*p3,*p4,*p5);
    return 0;
}
void star_str(int *p1,int *p2,int *p3,int *p4,int *p5,char *p)
{
 puts(p);
 for(;*p!='\n' && *p !='\0';p++)//?aà?2????′???′D′£??­?·ì??t???′è·?¨£?
 {
     if(isupper(*p))
      (*p1)++;
     else if(islower(*p))
       (*p2)++;
     else if(*p == ' ')
        (*p3)++;
     else if(isdigit(*p))
         (*p4)++;
     else
        (*p5)++;
 }
}

 

 

上一个:帮忙编写个学生成绩管理系统,只能用c语言,不能用C++!
下一个:计算机C语言

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,