题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。程序分析:利用while语句,条件为输入的字符不为'\n'.
1 回答
JustWannaHugU
TA贡献452条经验 获得超796个赞
//C语言: int main(){ char temp; while(getchar(temp)!='\n'){ if(number range){ numberNum++; }else if(charator range){ charactorNum++; }else if(space){ spaceNum++; }else { otherNum++; } } printf("Number:%d\nCharactor:%d\nSpace::%d\nOther:%d\n"); return 0; } //java版本: public class TestCount { private int i = 0; private int j = 0; private int k = 0; private int l = 0; private int m = 0; public void countChar(String s) { char[] arrayChar = s.toCharArray(); char c; for (int x = 0; x < arrayChar.length; x++) { c = arrayChar[x]; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { //判断英文字母 i++; } else if (c >= '\u4e00' && c <= '\u9fa5') { //判断中文 j++; } else if (c == ' ') { //判断空格 k++; } else if (c >= '0' && c < '9') { //判断数字 l++; } else { m++; } } System.out.println("i:" + i); System.out.println("j:" + j); System.out.println("k:" + k); System.out.println("l:" + l); System.out.println("m:" + m); } public static void main(String[] args){ TestCount tc = new TestCount(); tc.countChar("asdfasdf中 a 121 \\[][23423.,/"); } }
添加回答
举报
0/150
提交
取消