当前位置: 代码迷 >> 综合 >> C语言刷题(29):输入一行文字,找出其中大写字母,小写字母,空格,数字,以及其他字符各有多少?
  详细解决方案

C语言刷题(29):输入一行文字,找出其中大写字母,小写字母,空格,数字,以及其他字符各有多少?

热度:83   发布时间:2023-12-12 11:12:36.0

小憨笔记:
①在输入包含空格字符串的时候,不可使用scanf();
只能使用while((c[i]=getchar()!=’\n’) i++; 来输入。
②在给char类型的数组,赋值时,一定要在数组的最后附上’\0’,否则输出时,数组的剩下部分会出现乱码

#include <stdio.h>
#include <string.h>
void main()
{
    void copy(char *p,char *q,int m);void print(char *arr,int n);int countlen(char *a);void judge(char *arr,int n);char arr[100];int i=0;printf("input String:");while((arr[i]=getchar())!='\n')i++;arr[i]='\0';judge(arr,i);
}
void judge(char *arr,int n)
{
    int i,number=0,Lett=0,lett=0,space=0,other=0;for(i=0;*(arr+i)!='\0';i++){
    if(*(arr+i)<='9'&&*(arr+i)>='0'){
    number++;}else if(*(arr+i)<='Z'&&*(arr+i)>='A'){
    Lett++;}else if(*(arr+i)<='z'&&*(arr+i)>='a'){
    lett++;}else if(*(arr+i)==32){
    space++;}else{
    other++;}}printf("number=%d Letter=%d letter=%d space=%d other=%d",number,Lett,lett,space,other);
}
运行结果:
input String:aaaAAA...   133
number=3 Letter=3 letter=3 space=3 other=3