zoukankan      html  css  js  c++  java
  • 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果

    编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果

    题目解析:

    该题的关键在于要能够写出各种字符统计的条件

    代码示例:

    #include<stdio.h>
    
    int letter, digit, space, others;
    
    void CountChar(char str[])
    {
    	int i;
    	for (i = 0; str[i] != ''; i++)
    	{
            //统计字母
    		if ((str[i] >= 'a'&& str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) 
    			letter++;
    		else if (str[i] >= '0' && str[i] <= '9') //统计数字
    			digit++;
    		else if (str[i] == ' ')//统计空格
    			space++;
    		else
    			others++;  //统计其他字符
    	}
    }
    
    int main()
    {
    	char text[80];
    	printf("input string:
    ");
    	gets(text);
    	printf("string: %s
    ", text);
    
    	CountChar(text);
    	printf("
    letter:%d
    digit:%d
    space:%d
    others:%d
    ", letter, digit, space, others);
    	return 0;
    }
    

    运行结果:

    编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果

  • 相关阅读:
    四则运算02
    第三周学习进度条
    《构建之法》阅读笔记1
    第二周学习进度条
    四则运算01
    第八周进度条
    每日站立会日07,08
    每日站立会议06
    每日站立会议05
    每日站立会议04
  • 原文地址:https://www.cnblogs.com/inta/p/13356733.html
Copyright © 2011-2022 走看看