1009: C基础-统计字符

 

1009: C基础-统计字符

 

  • 题目描述:

 

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

  • 样例输入

a 1,
  • 样例输出

1
1
1
1
#include <iostream>
#include "stdio.h"
using  namespace std;
int main()
{
    char s;
    int i=0,j=0,k=0,m=0;

    while((s=getchar())!='\n')
    {

        if((s<='z'&&s>='a')||(s<'Z'&&s>'A'))i++;
        else if(s==' ') j++;
        else if(s<58&&s>47)k++;
        else m++;
    }
    cout<<i<<endl;
    cout<<j<<endl;
    cout<<k<<endl;
    cout<<m<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个修改版的程序,它会同时统计标题中数字字符和字母字符的个数: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <libxml/parser.h> #include <libxml/tree.h> // 统计字符串中数字字符的个数 int count_digit(char *str) { int count = 0; for (int i = 0; i < strlen(str); i++) { if (isdigit(str[i])) { count++; } } return count; } // 统计字符串中字母字符的个数 int count_alpha(char *str) { int count = 0; for (int i = 0; i < strlen(str); i++) { if (isalpha(str[i])) { count++; } } return count; } int main() { // 读取HTML文件 FILE *fp; char *filename = "a.htm"; fp = fopen(filename, "r"); if (fp == NULL) { printf("Failed to open file: %s\n", filename); return 1; } fseek(fp, 0L, SEEK_END); int filesize = ftell(fp); fseek(fp, 0L, SEEK_SET); char *html = malloc(filesize + 1); fread(html, sizeof(char), filesize, fp); fclose(fp); // 解析HTML文件 xmlDocPtr doc; xmlNodePtr node; doc = xmlReadMemory(html, filesize, "noname.xml", NULL, 0); if (doc == NULL) { printf("Failed to parse document\n"); return 1; } node = xmlDocGetRootElement(doc); // 找到标题内容 xmlNodePtr title_node; title_node = xmlGetFirstElementChild(node); while (title_node != NULL && strcmp((char *)title_node->name, "title") != 0) { title_node = xmlGetNextElementSibling(title_node); } if (title_node == NULL) { printf("Failed to find title\n"); return 1; } char *title = (char *)xmlNodeListGetString(doc, title_node->xmlChildrenNode, 1); // 找到行数 int lines = 0; char *pch = strchr(html, '\n'); while (pch != NULL) { lines++; pch = strchr(pch+1, '\n'); } // 统计标题中数字字符和字母字符的个数 int digit_count = count_digit(title); int alpha_count = count_alpha(title); // 写入结果文件 FILE *fp_out; char *filename_out = "result.txt"; fp_out = fopen(filename_out, "w"); if (fp_out == NULL) { printf("Failed to open file: %s\n", filename_out); return 1; } fprintf(fp_out, "a.htm网页的标题是:%s\n", title); fprintf(fp_out, "标题在文件 a.htm网页的第%d行\n", lines); fprintf(fp_out, "标题中数字字符的个数:%d\n", digit_count); fprintf(fp_out, "标题中字母字符的个数:%d\n", alpha_count); fclose(fp_out); // 释放内存 free(html); xmlFree(title); xmlFreeDoc(doc); return 0; } ``` 这个程序会读取名为a.htm的文件,解析其中的HTML代码,并找到标题内容和行数,同时统计标题中数字字符和字母字符的个数,然后将结果写入名为result.txt的文件中。你只需要将a.htm文件放在程序所在的目录下,然后编译运行程序即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值