【程序7】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1 function Statistics1(str) { 2 var strcount = 0; 3 var spacecount = 0; 4 var numcount = 0; 5 var othercount = 0; 6 7 for (var i = 0; i < str.length; i++) { 8 var tmpstr = str.charAt(i); 9 if (tmpstr >= 'a' && tmpstr <= 'z' || tmpstr >= 'A' && tmpstr <= 'Z') { 10 strcount++; 11 } 12 else if (tmpstr === ' ') { 13 spacecount++; 14 } 15 else if (tmpstr >= '0' && tmpstr <= '9') { 16 numcount++; 17 } 18 else { 19 othercount++; 20 } 21 } 22 return { "strcount": strcount, "spacecount": spacecount, "numcount": numcount, "othercount": othercount }; 23 }
1 function Statistics2(str) { 2 var strcount = 0; 3 var spacecount = 0; 4 var numcount = 0; 5 var othercount = 0; 6 var i = 0; 7 8 while (i < str.length) { 9 var tmpstr = str.charAt(i); 10 if (tmpstr >= 'a' && tmpstr <= 'z' || tmpstr >= 'A' && tmpstr <= 'Z') { 11 strcount++; 12 } 13 else if (tmpstr === ' ') { 14 spacecount++; 15 } 16 else if (tmpstr >= '0' && tmpstr <= '9') { 17 numcount++; 18 } 19 else { 20 othercount++; 21 } 22 i++; 23 } 24 return { "strcount": strcount, "spacecount": spacecount, "numcount": numcount, "othercount": othercount }; 25 }