题目要求:从屏幕输入一个字符串,然后输出字符串中英文字母的个数(空格不算在内). 如果说遇到空格就停止计算倒也好做,可题目要求遇到空格跳过去不计算.这就把我难住了,请高手指点. 这个是我做的: #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 !
----------------解决方案--------------------------------------------------------
//题目要求:从屏幕输入一个字符串,然后输出字符串中英文字母的个数(空格不算在内). //如果说遇到空格就停止计算倒也好做,可题目要求遇到空格跳过去不计算.这就把我难住了,请高手指点.
#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++;} 应该加个花括弧
----------------解决方案--------------------------------------------------------
if(isalpha(*str)) { count++; str++;} 应该加个花括弧
不对把?!
----------------解决方案--------------------------------------------------------
[此贴子已经被作者于2005-5-9 23:07:37编辑过]
----------------解决方案--------------------------------------------------------
果然是高手啊,在下佩服.
你们开发过什么工程吗?
不知C怎么开发呢.
----------------解决方案--------------------------------------------------------
哦!!
----------------解决方案--------------------------------------------------------