某公司的笔试题!

papaofdoudou
人工智能领域新星创作者
博客专家认证
2009-10-07 06:12:32
输入一句英文,写一个函数统计这句话中单词的数目,用C++或C实现,不要利用库函数。
记得以前学过谭浩强的那本C语言上面有这道题的源代码,我记得很清楚就写上了,不过总觉方法应该不止一个,这样短小精悍的题目在面试中经常被问及,为了有备无患,还请CSDN上的坛友把自己的算法用程序来实现,我想知道一些尽可能多的办法,谢谢了!
外加问一句,如果用STL来实现,有没有简单的容器类型和泛型算法??
...全文
866 51 打赏 收藏 转发到动态 举报
写回复
用AI写文章
51 条回复
切换为时间正序
请发表友善的回复…
发表回复
l3371073 2009-10-11
  • 打赏
  • 举报
回复
正解
int main()
{
char *str = "this is aaa,i am lwg. how are you?";
char *s = str;
int a = 0;
bool zimu = false;
char ch;
ch = s[0];
while(ch != '\0')
{

if(ch == ',' || ch == '!' || ch == '.' || ch == ' ' || ch == '?')
{
zimu = false;

}
else
{
if(!zimu)
{
a++;
}
zimu = true;
}
s++;
ch = s[0];

}
printf("%i", a);
return 0;
}
MYLOVEwm 2009-10-11
  • 打赏
  • 举报
回复
#include<stdio.h>
int main()
{char c[200];
int i=0,count=0,j=0;
gets(c);
while(c[i]!='\0')
{while(c[i]=='')
i++;
if(c[i]!='\0')
count++;
while(c[i]!='\0'&&c[i]!='')
j++;
printf("The words is:%d\n",j);
}
}
不说害怕 2009-10-11
  • 打赏
  • 举报
回复
i'm sorry..

这是几个单词
skipwitit 2009-10-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 deerwin1986 的回复:]
LZ写得貌似有错误。。。
C/C++ code
#include<iostream>
#include<cassert>
#include<cctype>usingnamespace std;int Count(constchar*str )
{
assert( str!= NULL );while(!isalpha(*str ) )
{++str;
}if(*str== NULL )
{return0;
}int result=1;while(*str!= NULL )
{if(!isalpha(*str ) )
{while(*str!= NULL&&!isalpha(*str ) )
{++str;
}if(*str!= NULL )
{++result;
}
}++str;
}return result;
}int main()
{
cout<< Count(" If,I love you BABY !" )<< endl;return0;
}

不信拿我程序里的例子试下。。。
[/Quote]
用到了库函数CCTYPE了
skipwitit 2009-10-10
  • 打赏
  • 举报
回复
我只会用容器

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{vector<string> vec;
string word;
while(cin>>word)
vec.push_back(word);
cout<<"输入单词个数为:"<<vec.size()<<endl;
return 0;

}






lihan6415151528 2009-10-09
  • 打赏
  • 举报
回复
标点符号应该处理
erduoyin 2009-10-09
  • 打赏
  • 举报
回复
42楼想的真周到....
cphj 2009-10-08
  • 打赏
  • 举报
回复
看来特殊场景还是很多的,补充一个:
This's a testing sentence, which has some duplicate words for testing words counting programs.
句子里面的This's应该算一个词
reneeland 2009-10-08
  • 打赏
  • 举报
回复

// LetterCount.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void WorldCount(const char *sentence,int &count)
{
char c;
bool flag=true;
for(int i=0;(c=sentence[i])!='\0';i++)
{
if(c==' ' )
flag=true;
else if(flag==true)
{
flag=false;
count++;
}
}
}

int main(int argc, char* argv[])
{
char string[81];
gets(string);
int number=0;

WorldCount(string,number);
printf("There are %d words in the line.\n",number);
return 0;
}


classic114112 2009-10-08
  • 打赏
  • 举报
回复
学习~~~~
  • 打赏
  • 举报
回复
挺简单的啊
细节问题不必太注重吧
有个思路
有些问题不具体实践光讨论
意义不大啊
reneeland 2009-10-08
  • 打赏
  • 举报
回复
学习来了~~~
small_well 2009-10-08
  • 打赏
  • 举报
回复

#include<stdio.h>
int main()
{
char c[100];
int i = 0,count = 0;

gets(c);
while(c[i]!='\0')
{
while(c[i] == ' ')
i++;
if(c[i]!='\0')
count++;
while(c[i]!='\0'&&c[i]!=' ')
i++;
}

printf("the words:%d\n",count);

return 0;
}
hittlle 2009-10-08
  • 打赏
  • 举报
回复
再来说一句: 首先,这儿不针对很极端的情况,比如"I,am", 这种情况,相信没有人能计算出有几个单词,因为中间没空格;其次,这儿的"单词"被定义成一个连续的串,比如,"I'm",算一个单词,在英语中,它确实算一个单词,因为它是缩写.再次,这儿的单词并不是英语语法意义上正确的单词,因为,碰到"I am--how to say"这种情况时,把"am--how"也当成一个单词了;这些呢都只是语法细节,加入些判断就OK了;
下面这个方法针对:句子开头和结尾有空格,并且句子中间有连续空格的情况下,统计单词数目
size_t countWords(char *str){
char *p = str, *q = str;
//消去开头的空格
while(*p==' ') p++;
//消去末尾的空格
while(*q)q++;
q--;
while(*q==' ')q--;

//开始统计
//如果中间有连续的空格,只算作一个空格
while(p!=q){
if(*p==' '){
while(*p==' ') p++;//若有连续空格,消去,只算作一个空格
count++;
}
p++;
}
return (count+1);
}
uldm1027 2009-10-08
  • 打赏
  • 举报
回复
那么简单的函数干吗不自己写.想学编程就自己写...不想编程,只是想混混.上面的随便拷贝个.
woods2001 2009-10-08
  • 打赏
  • 举报
回复
来个C++版本的

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string temp,output;
getline(cin,temp);
istringstream istring(temp);
int i=0;

while(istring>>output)
{
i++;
}

cout<<i<<endl;

}

C++比较方便
tfsict2008 2009-10-08
  • 打赏
  • 举报
回复
int count = 0;
boolean start = true;
for(int i = 0;i<allchars.length;i++){
if(allchars[i] == ' '){
if(start){
start = false;
count++;
}
}else{
if(!start)start = true;

}
}
if(start)count++;
return count;
qulcc 2009-10-08
  • 打赏
  • 举报
回复
我觉得对各种稀奇古怪的情况的讨论没太大意思。
只要有思路的就可以了,其他的都是语法细节
gueangyik 2009-10-08
  • 打赏
  • 举报
回复
用空格判断单词的数量 这个正确吗 ??
要是 一条句子“i,am”在这里 没有空格 但是2个单词。。

个人认为 只要是字母开始 遇到的不是字母的时候count++ 。。然后在遇到字母开头的就重复刚才前面的~~
naponstance 2009-10-08
  • 打赏
  • 举报
回复
前提要看句子的定义了,我觉得这里也就是指单词用空格隔开而形成的句子.
加载更多回复(31)
企业公司软件测试面试笔试题集合 软件测试面试题 (测试基础).doc 01_企业面试试卷(综合).doc 01_企业面试试卷(综合)_参考答案.doc 04_企业面试试卷(测试基础).doc 04_企业面试试卷(测试基础)_参考答案.doc 500强公司面试的经典正确与错误回答对比!!! 看看你的弱点.doc C面试题.txt Java初学者都必须知道的六大问题.doc 百度笔试题.txt 北京博彦科技笔试+面试.doc 北京大学计算机科学技术研究所.doc 波尔世通的笔试+面试.doc 测试人员面试题.doc 测试题.doc 常见的测试题(转贴).doc 传视数码公司的面试题.doc 汉端笔试题(7页).doc 合力金桥的笔试题.doc 华为面试题.doc 经典逻辑题.ppt 联合网视面试题.doc 美国英网软件公司题目.doc 面试考题(腾讯,招行等等).txt 某公司的面试试题.doc 奇虎面试题.doc 千像互动的笔试.doc 清华同方开发的面试题 (有兴趣的看一下了 !).doc 缺陷的等级划分,一个经常被问到的问题.doc 软件测试工程师笔试试题(大集合).doc 软件测试工程师测试试题大集合(二)包括答案.doc 软件测试工程师试题发布版.doc 软件测试试题.doc 软件评测复习知识点(小颖).doc 软通动力面试笔答.doc 瑞星笔试题(15道).doc 神州泰岳测试试题(笔试)转贴.doc 时力科技面试题.doc 瓦瑟笔试题(限男性).doc 喜安科 面试题.doc 性能计算公式.txt 亚控科技比试题.doc 一道测试notepad笔试题.doc 一道数据库的笔试题目.doc 一个外包测试公司笔试题!.doc 一家通讯公司的面试题目.doc 英文自我介绍大全.doc 英语面试.doc 英语面试题.doc 有意思的逻辑题.doc 中软的面试题(转贴).doc

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧