有关字符字符中英文大写字母、小写字母...个数的疑问
时间:2008-05-13 15:41:21
来源:论坛整理 作者: 编辑:chinaitzhe
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ //有三行文字,每行80个字符,统计其中英文大写字母、小写字母、数字、空格以及其他字符的个数 #include <Stdio.h> void main() { int i,j,upp,low,dig,spa,oth; char text[3][80]; upp=low=dig=spa=oth=0; for(i=0; i<3; i ) { printf("请输入第%d行:\n",i 1); gets(text[i]); for(j=0; j<80 && text[i][j]!='\0'; j )/*为什么要写text[i][j]!='\0',我知道\0代表null,不写的话下面统计其他字符会出错,这是为什么?*/ { if(text[i][j]>='A' && text[i][j]<='Z') { upp ; } else if(text[i][j]>='a' && text[i][j]<='z') { low ; } else if(text[i][j]>='0' && text[i][j]<='9') { dig ; } else if(text[i][j]==' ') { spa ; } else { oth ; } } } printf("大写字母:%d\n",upp); printf("小写字母:%d\n",low); printf("数字:%d\n",dig); printf("空格:%d\n",spa); printf("其他:%d\n",oth); }
网友回复:因为字符串结束符是 '\0'
网友回复:字符串是以'\0'为结束符的
网友回复:ding
网友回复:C风格字符串都是以'\0'结尾滴
网友回复:字符串结束的标志
并不是每句都要有80个字符,没有该句程序结果将出错。
网友回复:楼上的都是正解,字符串结束符,用以表示字符串结束.
网友回复:\'0'是C的结束语
网友回复:/*为什么要写text[i][j]!='\0',我知道\0代表null,不写的话下面统计其他字符会出错,这是为什么?*/
这个是作为循环的结束标志
假如你不停访问内存
当溢出的时候,程序就会崩掉的
网友回复:我写的统计其中英文字母、数字、空格以及其他字符的个数,并输出:
/*输入一行字符,分别统计其中字母,空格,数字和其他字符的个数,并分别打印*/
#include <stdio.h>
#include <conio.h>
#define TRUE 1
#define FLASE 0
int gp_letter(int);
int gp_number(int);
int main(void)
{
char c_ch;
int b_num=FLASE;
int n_space=0,n_number=0,n_letter=0,n_other=0;
char number[80],letter[80],other[80];
char *p_number=number,*p_letter=letter,*p_other=other;
printf("Please inputs some words:\n");
while((c_ch=getchar())!='\n')
{
if(gp_number(c_ch) ¦ ¦('.'==c_ch&&b_num))
{
*(p_number )=c_ch;
b_num=TRUE;
continue;
}
if(' '==c_ch)
n_space ;
else
if(gp_letter(c_ch))
{
n_letter ;
*(p_letter )=c_ch;
*(p_letter )=' ';
}
else
{
n_other ;
*(p_other )=c_ch;
*(p_other )=' ';
}
if(b_num)
{
n_number ;
*(p_number )=' ';
b_num=FLASE;
}
}
*p_number=0,*p_letter=0,*p_other=0;
printf("The numbers is %d,they are : %s\nThe letters is %d,they are : %s\nThe space is %d\nThe other is %d,they are : %s\n",
n_number,number,n_letter,letter,n_space,n_other,other);
getch();
return 0;
}
int gp_letter(int n)
{
int b_word=FLASE;
if((n>='a'&&n <='z') ¦ ¦(n>='A'&&n <='Z'))
b_word=TRUE;
return b_word;
}
int gp_number(int n)
{
int b_number=TRUE;
switch(n)
{
case '0':break;
case '1':break;
case '2':break;
case '3':break;
case '4':break;
case '5':break;
case '6':break;
case '7':break;
case '8':break;
case '9':break;
default :b_number=FLASE;
}
return b_number;
}
网友回复:
网友回复:谢谢了
关键字:字符,字符,中英文,大写字母,小写字母,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面