c - 统计字符串"字母,空格,数字,其他字符"的个数和行数.

 1 #include <stdio.h>
 2 #include <ctype.h>
 3
 4 using namespace std;
 5
 6 /*
 7 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
 8 */
 9
10 void
11 count() {
12     //统计个数.
13     int letters = 0;
14     int spaces = 0;
15     int digit = 0;
16     int others = 0;
17     char curChar;
18     //注意的是,对(一行中)逐个字符进行读取时,‘\n‘对应ASCII值为10,而不是0,所以需要跟‘\n‘判断(不同于逐句判断).
19     while((curChar = getchar()) != ‘\n‘) {
20         if(isalpha(curChar))    //检查参数curChar是否为英文字母,在标准c中相当于使用“isupper(curChar)||islower(curChar)”
21             ++letters;
22         else if(isdigit(curChar))    //检查参数curChar是否为阿拉伯数字0到9.
23             ++digit;
24         else if(isspace(curChar))
25             ++spaces;
26         else ++others;
27     }
28
29     printf("letters:%d, digits:%d, spaces:%d,others:%d\n", letters, digit, spaces, others);
30     //cout<<"letters:"<<letters<<",digits:"<<digit<<",spaces:"<<spaces<<",others:"<<others<<endl;
31 }
32
33 //统计行数.
34 int
35 countLines(char *input) {
36     int lns = 0;
37     while(gets(input))
38         ++lns;
39     return lns;
40 }
41
42 int
43 main(void) {
44     printf("enter a string:");
45     count();
46
47     //char *t;
48     //gets(t);
49     //Run-Time Check Failure #3 - The variable ‘t‘ is being used without being initialized.
50     /*
51     值得注意的是,如果不小心传递给gets函数的参数是为开辟空间的指针变量‘t‘,会报以上的异常.其实原因也很简单,t没有得到内存空间(即没有指向内存中的合法空间),放到gets中自然不能被使用.
52     */
53
54     char cs[10240];
55     int lns = countLines(cs);
56     printf("lines:%d\n", lns);
57 }
时间: 2024-08-06 22:39:15

c - 统计字符串"字母,空格,数字,其他字符"的个数和行数.的相关文章

OJ刷题之《统计出其中英文字母、数字、空格和其他字符的个数》

题目描述 输入一行字符,分别统计出其中英文字母.数字.空格和其他字符的个数. 输入 一行字符 输出 统计值 样例输入 aklsjflj123 sadf918u324 asdf91u32oasdf/.';123 样例输出 23 16 2 4 代码如下: #include <iostream> #include <cstdio> using namespace std; int main() { char str[50]; int i=0,n1=0,n2=0,n3=0,n4=0; ge

烟大ACM新秀赛 B统计出其中英文字母、数字、空格和其他字符的个数

Description 输入一行字符,分别统计出其中英文字母.数字.空格和其他字符的个数. Input 一行字符 Output 统计值 Sample Input aklsjflj123 sadf918u324 asdf91u32oasdf/.';123 Sample Output 23 16 2 4 HINT /* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称: .cpp *作 者: 冷基栋 *完成日期:2014年3月

统计输入任意的字符中中英文字母,空格和其他字符的个数 python

这里用到了三个函数: #判断是否为数字:str.isdigit()#是否为字母:str.isalpha()#是否为空格:str.isspace() def tongji(str): alpha = 0 number = 0 space =0 qt = 0 for i in range(len(str)): #或者for i in str: if str[i].isalpha(): #接上一句改为:i.isalpha() alpha += 1 elif str[i].isdigit(): numb

字符串里有数字和字符,如何只获取一种(以数字为例)

随便给你一个含有数字的字符串,比如: String s="eert343dfg56756dtry66fggg89dfgf"; 那我们如何把其中的数字提取出来呢?大致有以下几种方法,正则表达式,集合类,还有就是String类提供的方法. 1 String类提供的方法: package 测试练习;import java.util.*;public class get_StringNum { /** *2012.6.2 */ public static void main(String[]

统计python文件中的代码,注释,空白对应的行数

其实代码和空白行很好统计,难点是注释行 python中的注释分为以#开头的单行注释 或者以'''开头以'''结尾 或以"""开头以"""结尾的文档注释,如: ''' hello world '''和 ''' hello world''' 思路是用is_comment记录是否存在多行注释,如果不存在,则判断当前行是否以'''开头,是则将is_comment设为True,否则进行空行.当前行注释以及代码行的判断,如果is_comment已经为True

输入任意长的一个字符串,统计其字母、数字、空格及其他字符的数量。

思路:简单的利用一个多重 if 结构就可以解决. CODE: import java.util.Scanner; public class Character{ public static void main(String[] args){ System.out.println ("请输入一个字符串:"); Scanner ss = new Scanner(System.in); String sc = ss.nextLine(); char[] ch= sc.toCharArray(

c语言输入一个字符串,统计其字母,数字和其他字符的个数,并以柱状图输出

#include<stdio.h>int main(int arge,char *argv[]){ char ch; int alp=0,num=0,oth=0,len,alp_start,num_start,oth_start,i; while((ch=getchar())!=-1) { if(ch>='0'&&ch<='9') num++; else if((ch>='a'&&ch<='z')||(ch>='A'&&am

输入字符串,统计其中数字,空格和其他字符的个数

#define _CRT_SECURE_NO_WARNINGS#include<stdio.h>int main(){ char a; int num = 0; int space = 0; int other = 0; while ((a = getchar()) != '\n') {  if ((a >= '0') && (a <= '9'))   num++;  else if (a == ' ')   space++;  else   other++; } 

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

题目描述 输入 一行字符 输出 统计值 样例输入 aklsjflj123 sadf918u324 asdf91u32oasdf/.';123 样例输出 23 16 2 4程序:#include<stdio.h>int main(){    char s;    int d=0,e=0,k=0,o=0;    while((s=getchar())!='\n')     {         if((s>='A'&&s<='Z')|(s>='a'&&