| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 385 人关注过本帖
标题:[求助]输出字符串个数的一题.
收藏  订阅  推荐  打印 
zker
Rank: 2
等级:注册会员
帖子:85
积分:950
注册:2005-2-3
[求助]输出字符串个数的一题.

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

2005-5-9 12:41
musicml
Rank: 3Rank: 3
等级:中级会员
帖子:273
积分:2830
注册:2005-4-2

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

#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); }


Every thing is possible.
2005-5-9 13:47
zker
Rank: 2
等级:注册会员
帖子:85
积分:950
注册:2005-2-3

谢谢 musicml !

------------希望与大家一起进步!------------
2005-5-9 20:56
牛虻
Rank: 3Rank: 3
等级:中级会员
威望:1
帖子:472
积分:4870
注册:2004-10-1

以下是引用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
musicml
Rank: 3Rank: 3
等级:中级会员
帖子:273
积分:2830
注册:2005-4-2

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

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

不对把?!


Every thing is possible.
2005-5-9 22:46
牛虻
Rank: 3Rank: 3
等级:中级会员
威望:1
帖子:472
积分:4870
注册:2004-10-1

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

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


土冒
2005-5-9 23:02
深夜狼
Rank: 3Rank: 3
来自:广西桂林
等级:中级会员
帖子:347
积分:3574
注册:2005-5-9

果然是高手啊,在下佩服.

你们开发过什么工程吗?

不知C怎么开发呢.
2005-5-9 23:17
musicml
Rank: 3Rank: 3
等级:中级会员
帖子:273
积分:2830
注册:2005-4-2

哦!!

Every thing is possible.
2005-5-10 00:51
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.077795 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved