统计字符串中字母,数字,空格以及其他字符的个数。
2017-12-09 20:51
573 查看
题目描述
输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
输入
一行字符
输出
统计值
样例输入
样例输出
#include <stdio.h>
int main(){ int letters = 0; int spaces = 0; int nums = 0; int others = 0; char ch; while ((ch = getchar ()) != '\n') { if ((ch>='a' && ch <= 'z') || (ch>='A'&&ch<='Z')) letters++; else if (ch>='0' && ch <='9') nums++; else if (ch == ' ') spaces++; else others++; } printf ("%d %d %d %d\n", letters,nums,spaces,others); return 0;}Java写法import java.util.Scanner;public class Main{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int count_letters = 0; int count_nums = 0; int count_spaces = 0; int count_others = 0; for(int i = 0;i < s.length();i++) { if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')) { count_letters++; } else if(s.charAt(i) >= '0' && s.charAt(i) <= '9')//注意这个地方也有加上单引号,此处将数字当作字符处理。 { count_nums++; } else if(s.charAt(i) == ' ') { count_spaces++; } else { count_others++; } } System.out.print(count_letters+" "+count_nums+" "+count_spaces+" "+count_others); }}
输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
输入
一行字符
输出
统计值
样例输入
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
样例输出
23 16 2 4
C语言写法
#include <stdio.h>
int main(){ int letters = 0; int spaces = 0; int nums = 0; int others = 0; char ch; while ((ch = getchar ()) != '\n') { if ((ch>='a' && ch <= 'z') || (ch>='A'&&ch<='Z')) letters++; else if (ch>='0' && ch <='9') nums++; else if (ch == ' ') spaces++; else others++; } printf ("%d %d %d %d\n", letters,nums,spaces,others); return 0;}Java写法import java.util.Scanner;public class Main{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int count_letters = 0; int count_nums = 0; int count_spaces = 0; int count_others = 0; for(int i = 0;i < s.length();i++) { if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')) { count_letters++; } else if(s.charAt(i) >= '0' && s.charAt(i) <= '9')//注意这个地方也有加上单引号,此处将数字当作字符处理。 { count_nums++; } else if(s.charAt(i) == ' ') { count_spaces++; } else { count_others++; } } System.out.print(count_letters+" "+count_nums+" "+count_spaces+" "+count_others); }}
相关文章推荐
- 习题 7.9 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格、和其他字符的个数,在主函数中输入字符串以及输出上述的结果。
- 编写一个函数,由实参传来一个字符串,统计此字符串中字母,数字,空格,和其他字符的个数,在主函数中输入字符串以及输出上述统计的结果。再考虑将算的的结果放在一个数组中
- 编写一个函数,从实参传来一个字符串,统计其中字母,数字,空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果
- 输入一行字符串分别统计出字母。数字。空格和其他字符 *利用while循环语句条件输入的字符为"\n"
- 编写一个函数,分别统计出传入字符串参数(可能不止一个参数)的英文字母,空格,数字和其他字符的个数
- 输入一个字符串,分别统计出包含的英文字母、数字、空格和其他字符的个数
- 统计传入字符串参数(可能不止一个参数)的英文字母、空格、数字和其他字符的个数
- 《任意输入一个合理大小的字符串,实现逆序输出并统计一个字符串中字母、数字、空格及其他字符的个数》
- 编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
- 统计字符串中的字母,数字,空格和其他字符的个数---指针
- 统计字符串中字母,数字,空格和其他字符的个数。
- 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数
- c - 统计字符串"字母,空格,数字,其他字符"的个数和行数.
- (c++)编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
- Ex7_4编写函数,统计此字符串中字母、数字、空格和其他字符­的个数
- 写函数,计算传入字符串中单个数字、字母、空格以及 其他字符的个数
- 编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
- 编写一函数,由实参数来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述的结果
- 8.9 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,并输出结果。(改)
- 有3行文字,每行有80个字符,分别统计出其中大小写字母,数字,空格以及其他字符个数