| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 580 人关注过本帖
标题:问两个问题
只看楼主 加入收藏
whoaiwo310
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-3-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
问两个问题
1.编写一个函数,输入一行字符,将此字符串中最长的单词输出.
2.在唐浩强的那本书中讲算法的特性中有一条是“有零个或多个输入”,我想问的是----是不是不存在一个输入这种情况?
搜索更多相关主题的帖子: 单词 函数 字符串 
2010-03-29 21:48
jd205
Rank: 2
等 级:论坛游民
帖 子:139
专家分:69
注 册:2006-10-26
收藏
得分:0 
1、你可以判断当前空格与它相邻的前一个空格之间的距离,看看哪个最大,那个最大单词就出来。(前提:你所谓的字符串中,单词之间用空格间隔)
或者
把里面单词取出扔到string里,用string.length()就出来。
方法多想想吧··

2、没看懂···
2010-03-29 21:54
whoaiwo310
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-3-16
收藏
得分:0 
关于第一问我知道方法 但是实现起来感觉很困难。第二问是关于唐浩强P19面的算法 第三条特性
2010-03-29 22:17
whoaiwo310
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-3-16
收藏
得分:0 
哪个高手帮我解决下啊
2010-03-30 09:16
hxlich
Rank: 2
等 级:论坛游民
帖 子:26
专家分:45
注 册:2010-3-28
收藏
得分:20 
以下是引用whoaiwo310在2010-3-29 22:17:54的发言:

关于第一问我知道方法 但是实现起来感觉很困难。第二问是关于唐浩强P19面的算法 第三条特性

写了个大概,希望有帮助
char buf[1024+1];                          // buffer to save your string
int iLetter = temp = 0;                    // iLetter to save letter number of words
char *ptr;                                 // store the first letter addr of the longest word
ptr=buf[0];                                // initialize to the first in the array buf   
for( i =0; i< sizeof (buf) ;i++)
{
    if( ! buf[i]==' ')                     // if not a space   
    {
        ++iLetter;                         // iLetter increment   
        if(iLetter > temp)                // if iLetter big than the longest word so far
            temp = iLetter;                // store the recent word as the longest
     }
     else                                  // if a space
     {   
        if(iLetter == temp)                // the word is over so check if it is longest word so far      
            ptr =buf+i-iLetter;            // if true store the first letter of the word in ptr
        iLetter = 0;                       // reset iLetter
     }
}
2010-03-30 09:43
Cy_Chance
Rank: 1
等 级:新手上路
帖 子:46
专家分:1
注 册:2007-10-15
收藏
得分:0 
strtok //分割
 strlen  //比较
 strcpy //拷贝
2010-03-30 10:12
hxlich
Rank: 2
等 级:论坛游民
帖 子:26
专家分:45
注 册:2010-3-28
收藏
得分:0 
回复 6楼 Cy_Chance
malloc
2010-03-30 10:21
快速回复:问两个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020728 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved