以下是引用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
}
}