当前位置: 代码迷 >> C语言 >> [求助]输出字符串个数的一题.
  详细解决方案

[求助]输出字符串个数的一题.

热度:160   发布时间:2005-05-09 12:41:00.0
[求助]输出字符串个数的一题.

题目要求:从屏幕输入一个字符串,然后输出字符串中英文字母的个数(空格不算在内). 如果说遇到空格就停止计算倒也好做,可题目要求遇到空格跳过去不计算.这就把我难住了,请高手指点. 这个是我做的: #include "stdio.h" #include "string.h"

main() { char a[100]; int i=0,n=0; printf("Please enter a string:\n"); gets(a); while (a[n]!='\0') { if (('a'<=a[n]<='z')||('A'<=a[n]<='Z')) n++; i++; } printf("n=%d\n",n); getch(); }

搜索更多相关的解决方案: 输出  字符  

----------------解决方案--------------------------------------------------------

//题目要求:从屏幕输入一个字符串,然后输出字符串中英文字母的个数(空格不算在内). //如果说遇到空格就停止计算倒也好做,可题目要求遇到空格跳过去不计算.这就把我难住了,请高手指点.

#include <stdio.h> #include <ctype.h> #include <string.h>

#define MAXSIZE 100

void output_string(char *);

void output_string(char * str) { int i; int count; int length; i=0; count=0; length=strlen(str); while(*str!='\0') { while(isspace(*str)) str++; if(isalpha(*str)) count++; str++; } printf("the alphabet's number is %d :\n",count); }

void main() { char arry[MAXSIZE]; printf("please enter the string :\n"); gets(arry); output_string(arry); }


----------------解决方案--------------------------------------------------------
谢谢 musicml !
----------------解决方案--------------------------------------------------------
以下是引用musicml在2005-5-9 13:47:19的发言:

//题目要求:从屏幕输入一个字符串,然后输出字符串中英文字母的个数(空格不算在内). //如果说遇到空格就停止计算倒也好做,可题目要求遇到空格跳过去不计算.这就把我难住了,请高手指点.

#include <stdio.h> #include <ctype.h> #include <string.h>

#define MAXSIZE 100

void output_string(char *);

void output_string(char * str) { int i; int count; int length; i=0; count=0; length=strlen(str); while(*str!='\0') { while(isspace(*str)) str++; if(isalpha(*str)) count++; str++; } printf("the alphabet's number is %d :\n",count); }

void main() { char arry[MAXSIZE]; printf("please enter the string :\n"); gets(arry); output_string(arry); }

if(isalpha(*str)) { count++; str++;} 应该加个花括弧


----------------解决方案--------------------------------------------------------
以下是引用牛虻在2005-5-9 21:40:20的发言:

if(isalpha(*str)) { count++; str++;} 应该加个花括弧

不对把?!


----------------解决方案--------------------------------------------------------
以下是引用musicml在2005-5-9 22:46:54的发言: 不对把?!
不好意思,我看错了,上面一句是while(isspace(*str)),我看成了if了,嘿嘿,说回来,这样做效率比if高

[此贴子已经被作者于2005-5-9 23:07:37编辑过]


----------------解决方案--------------------------------------------------------
果然是高手啊,在下佩服.

你们开发过什么工程吗?

不知C怎么开发呢.
----------------解决方案--------------------------------------------------------
哦!!

----------------解决方案--------------------------------------------------------
  相关解决方案