帮小弟我写一个小程序 多谢
【求助】帮我写一个小程序 谢谢
要求:有一篇文章,共有3行文字,每行有个80字符.要求分别统计出其中英文大写字母、小写字母、空格以及其它字符的个数.
*这道题是 谭浩强 第三版《C程序设计》上的一道习题。我想过了也试过了没有成功,后来看答案也没有搞懂.
------最佳解决方案--------------------
#include <stdio.h>
//const char* sample= "dflks;ldg GtrT fHRTsdwffS DfH!@$3 er4";
int main( void )
{
int nUpper, nLower, nSpace, nOther;
char linebuf[1024];
FILE* fp = fopen("a.txt","r"); // 打開你的文件
if (!fp) {
printf("not found file.\n");
return 0;
}
nUpper = nLower = nSpace = nOther = 0;
while (fget(linebuf, sizeof(linebuf), fp)) // 讀取文件中的一行, 不超過1023個字符。
{
int i = 0;
char ch = 0;
while(ch = linebuf[i++]){
if ('A'<=ch && ch <= 'Z') //大寫字母
nUpper++;
else if ('a'<=ch && ch <= 'z') //小寫字母
nLower++;
else if (ch == ' ') // 空格
nSpace++;
else //其他字符
nOther++;
}
}
printf("Upper=%d, Lower=%d, Space=%d, Other=%d\n",nUpper, nLower, nSpace, nOther);
fclose(fp);
return 0;
}
------其他解决方案--------------------
你把你看不懂的那个答案贴出来,有什么不懂的地方再问问。这样直接要代码不好。
如果是我写的话,思路是这样的:定义4个int变量用于记录每种字符的个数,将三行文字放进一个字符串里,然后依次对每个字符进行判断(英文大写字母、小写字母、空格还是其它字符)大小写可以用isupper islower,空格可以 == " ";每判断一个给做记录用的变量加1。最后就会有每种字符的个数了。
------其他解决方案--------------------
还是拿不懂的地方说吧,那样快
要求:有一篇文章,共有3行文字,每行有个80字符.要求分别统计出其中英文大写字母、小写字母、空格以及其它字符的个数.
*这道题是 谭浩强 第三版《C程序设计》上的一道习题。我想过了也试过了没有成功,后来看答案也没有搞懂.
------最佳解决方案--------------------
#include <stdio.h>
//const char* sample= "dflks;ldg GtrT fHRTsdwffS DfH!@$3 er4";
int main( void )
{
int nUpper, nLower, nSpace, nOther;
char linebuf[1024];
FILE* fp = fopen("a.txt","r"); // 打開你的文件
if (!fp) {
printf("not found file.\n");
return 0;
}
nUpper = nLower = nSpace = nOther = 0;
while (fget(linebuf, sizeof(linebuf), fp)) // 讀取文件中的一行, 不超過1023個字符。
{
int i = 0;
char ch = 0;
while(ch = linebuf[i++]){
if ('A'<=ch && ch <= 'Z') //大寫字母
nUpper++;
else if ('a'<=ch && ch <= 'z') //小寫字母
nLower++;
else if (ch == ' ') // 空格
nSpace++;
else //其他字符
nOther++;
}
}
printf("Upper=%d, Lower=%d, Space=%d, Other=%d\n",nUpper, nLower, nSpace, nOther);
fclose(fp);
return 0;
}
------其他解决方案--------------------
你把你看不懂的那个答案贴出来,有什么不懂的地方再问问。这样直接要代码不好。
如果是我写的话,思路是这样的:定义4个int变量用于记录每种字符的个数,将三行文字放进一个字符串里,然后依次对每个字符进行判断(英文大写字母、小写字母、空格还是其它字符)大小写可以用isupper islower,空格可以 == " ";每判断一个给做记录用的变量加1。最后就会有每种字符的个数了。
------其他解决方案--------------------
还是拿不懂的地方说吧,那样快