注册 登录
编程论坛 C语言论坛

大佬们帮忙看一下

第五先生 发布于 2020-09-04 15:38, 2043 次点击
【问题描述】输入多行字符串(不超过20行,每行字符串长度不超过100个字符),统计每行字符串中字母个数(包括大小写字母)、数字个数、其它字符个数。
【输入形式】第一行输入字符串行数n,第二行开始输入各字符串。字符串中可包括任意的字符,如:AQ 12 #@  567 +/
【输出形式】每行输出对应字符串行中字母、数字、其它字符个数,个数值之间以一个空格间隔。
【样例输入】

3

123 Yy& 90

123

YHUC##)(123

【样例输出】

2 5 3

0 3 0

4 3 4
【样例说明】输入三行字符串,分别是第一行的:123 Yy& 90,第二行的:123,第三行的YHUC##)(123。输出各行包括的字母、数字、其它字符的个数。

【评分标准】程序中包含指针的使用

5 回复
#2
rjsp2020-09-04 16:57
程序代码:
#include <stdio.h>

int main( void )
{
    unsigned linenum;
    scanf( "%u%*[^\n]", &linenum );
    scanf( "%*c" );

    unsigned lineindex = 0;
    unsigned alpha=0, digit=0, others=0;
    for( int ch; ch=getchar(); )
    {
        if( ch == '\n')
        {
            printf( "%u %u %u\n", alpha, digit, others );
            if( ++lineindex == linenum )
                break;
            alpha=0, digit=0, others=0;
        }
        else if( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z') )
            ++alpha;
        else if( ch>='0' && ch<='9' )
            ++digit;
        else
            ++others;
    }
}
#3
第五先生2020-09-04 17:24
回复 2楼 rjsp
也是for循环有问题
#4
apull2020-09-04 20:50
程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> //判断字符类型需要的头文件

int main(void)
{
    unsigned linenum;
    scanf("%u%*[^\n]", &linenum);
    scanf("%*c");

    unsigned alpha[20] = {0}, digit[20] = {0}, others[20] = {0};

    char str[100], *p;
    int i;

    for(i = 0; i < linenum; i++)
    {
        gets(str);
        p = str;

        while(*p != 0)
        {
            if(isdigit(*p))            
                digit[i]++;        
            else if(isalpha(*p))
                alpha[i]++;
            else
                others[i]++;

            p++;
        }
    }

    for(i = 0; i < linenum; i++)
    {
        printf("%u %u %u\n", alpha[i], digit[i], others[i]);
    }

    system("pause");

    return 0;
}
#5
纯蓝之刃2020-09-05 21:27
程序代码:
#include <stdio.h>

int main(void)
{
    unsigned int num,count=0;
    unsigned int alpha[20]={0},digit[20]={0},others[20]={0};
    char ch;
    scanf("%u",&num);
    while(getchar()!='\n');

    while(1)
    {
        ch=getchar();
        if(ch=='\n')
        {
            count++;
            if(count>=num)
                break;
        }
        else if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
            alpha[count]++;
        else if(ch>='0'&&ch<='9')
            digit[count]++;
        else
            others[count]++;
    }

    for(count=0;count<num;count++)
        printf("%u %u %u\n",alpha[count],digit[count],others[count]);

    return 0;
}
#6
hellobird2020-09-08 02:00
程序代码:

#include <stdio.h>
#include <ctype.h>
/*
  JeffLee 2020年9月8日01:58:56
 */
int main(void)
{
    char str[100] = {'\0'};
    unsigned alpha[20] = {0},
             digit[20] = {0},
            others[20] = {0};
    int i, j, k;

    for (i = 0; i < 20; i++) {
        fgets(str, 100, stdin);
        if (str[0] == '\n') break;
        for (j = 0; str[j] != '\0'; j++) {
            if (str[j]=='\n') {
                continue;
            }
            else if (isalpha(str[j])) {
                alpha[i]++;
            } else if (isdigit(str[j])) {
                digit[i]++;
            } else {
                others[i]++;
            } //end if
        } //end for j
    } //end for i

    //outputs
    for (k = 0; k < i; k++) {
        printf("%u\t"
               "%u\t"
               "%u\n",
               alpha[k],
               digit[k],
               others[k]
            );
    }
    return 0;
}

只有本站会员才能查看附件,请 登录
1