| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5550 人关注过本帖
标题:如何将一行字符串中最长的单词输出
只看楼主 加入收藏
风之语录
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2006-3-15
收藏
得分:0 
没有看明白,你没说清楚。是有多个字符串吗》?

2006-03-31 16:49
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 

# include <stdio.h>
# include <stdlib.h>

struct node
{
char lettle[20];/* 定义结构体 */
int amount;
struct node *next;
} ;

struct node *add(struct node *head,char a[],int x)/* 添加链表 */
{
struct node *p;
static struct node *q;
int i=0;
p=(struct node *)malloc(sizeof(struct node));
p->amount=x; p->next=0;
for(i=0;i<=x;i++)
p->lettle[i]=a[i];

if (head==0) head=q=p;
else { q->next=p ; q=q->next;}

return(*head);
}


void print(struct node *head) /* 求最长的字符串.并打印 */
{
struct node *k,*l;

if (head==0){ printf("there is nothing"); return;}
l=head;
k=head->next;

while(k==0)
{ if ((l->amount)<(k->amount))
l=k;
k=k->next;
}

printf("\n%s,%d",l->lettle,l->amount) ;

return;
}


main()
{
char words[100],a[20];
struct node *head=0;
int i=0,j,m,n,x,count;

gets(words);/* 得到字符串 */

while(words[i]!='\0')
{
if(words[i]==' ')/* 以空格为隔字符 */
count=0;
else {
count=1;m=i;}/* m为 单词的第一个字符的位置 */

if (count=1)

{for(j=i;words[j]!='\0';j++)

{ if(words[j]==' ')
{i=j-1; n=j ; break; }/* 记n为 单词的最后一个字符,应该是最后一个字符+1 */
n=j; }

for(x=0;m<=n;m++,x++)

a[x]=words[m] ;/* 把这个单词赋给 a */

a[x]='\0';

head=add(head,a,x); }/* 添加到链表中 */

i++;
}

print(head);/* 打印 */
getch();

}


我通过编译..但运行后出错..算法应该没问题..哪的毛病呢~~大家来找找.

斑竹..帮忙弄弄~~~~~


改了一下..现在输入一个单词 可以计算出长度..可以碰上 空格 就没反映了..怎么回事~~

[此贴子已经被作者于2006-3-31 18:55:37编辑过]


[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-03-31 17:50
限量版猪头
Rank: 2
等 级:论坛游民
威 望:1
帖 子:165
专家分:30
注 册:2006-2-5
收藏
得分:0 
2楼的对~肯定以某个字符为间隔~以那个字符为结束标记~
然后算出长度~下面不用说了~

2006-03-31 21:13
feng1256
Rank: 4
等 级:贵宾
威 望:14
帖 子:2899
专家分:0
注 册:2005-11-24
收藏
得分:0 

对12楼的程序有许多问题,所了些修改,只针对这程序,不针对楼主这个题
楼主这题有比较简单的方法,例如用一个数组来放最大的那个单词就行
用链表显然是麻烦了
[CODE]
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <string.h>
struct node
{
char lettle[20];
int amount;
struct node *next;
};

struct node *Add(struct node *head,char a[],int x)
{
struct node *p;
static struct node *q;

p=(struct node *)malloc(sizeof(struct node));
p->amount=x;
p->next=NULL;
strcpy(p->lettle,a); /*做了修改*/
if (head==NULL) /* NULL和0是有区别的,一般别这样写*/
head=q=p;
else
{
q->next=p;
q=q->next;
}
return(head);
}

void Print(struct node *head)
{
struct node *k,*l;

if (head==NULL)
{
printf("There is nothing!\n");
return;
}
l=head;
k=head->next;

while(k)
{

if ( (l->amount) < (k->amount) )
l=k;
k=k->next;
}
printf("\n%s,%d\n",l->lettle,l->amount) ;

}

void main()
{
char words[100],a[20];
struct node *head=NULL,*tail;
int i=0,j,m,n; /*变量不需要那么多*/

gets(words);
while(words[i]!='\0')
{
if( !isalpha(words[i]) ) /*这样改,可以以任何非字符符号做单词间隔符*/
i++;
else /*里面做了一些简化*/
{
m=i;
while( isalpha(words[i]) )
i++;
for(n=0;m<i;m++,n++)
a[n]=words[m] ;
a[n]='\0';
head=Add(head,a,n);
i++;
}
}

Print(head);
while(head) /*这还是很必要的*/
{
tail=head;
head=head->next;
free(tail);
}
getch();
}



[/CODE]


叁蓙大山:工謪、稅務、嗣發 抱歉:不回答女人的问题
2006-04-01 00:10
feng1256
Rank: 4
等 级:贵宾
威 望:14
帖 子:2899
专家分:0
注 册:2005-11-24
收藏
得分:0 
/*这样改,可以以任何非字母字符做单词间隔符*/


叁蓙大山:工謪、稅務、嗣發 抱歉:不回答女人的问题
2006-04-01 00:23
油豆
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2006-3-29
收藏
得分:0 
不用这么麻烦的.定义一个字符指针p指向最大的那个单词,定义一个整型变量long存其长度.找到一个单词就统计其长度,与long比较,比long大就修改p,使其指向这个单词,修改long存当前长度.程序完成后输出p所指向的那个单词就可以了.

[此贴子已经被作者于2006-4-1 7:02:31编辑过]



2006-04-01 07:02
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
main()
{
char word[100],compare[20],record[20];
int i,m,n,x,k;
gets(word);
i=0; k=0;
while(word[i]!='\0')

if(!isalpha(word[i]))
i++;

else { m=i;
while(isalpha(word[i]))

i++;

for(x=0;m<i;x++,m++)
compare[x]=word[m];
compare[x]='\0';
if(k<x)
{ strcpy(record,compare) ;
k=x;}

}

printf("%s,%d",record,k);

getch();
}

按版主的意思..改成这样~~

有临仿之嫌哦.!~~


[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-04-01 09:56
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 
楼上的.别光说不练..写出来~

[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-04-01 10:01
油豆
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2006-3-29
收藏
得分:0 
呵呵,晚上写给你.

2006-04-01 12:23
油豆
Rank: 1
等 级:新手上路
帖 子:67
专家分:0
注 册:2006-3-29
收藏
得分:0 
#include <stdio.h>
main()
{
char str[100],*p,*max,*q;//max为最长单词的首地址.
int l1=0,l; //long为最长单词的长度.
gets(str);
p=str;
while(!((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')))
p++;
max=p; //找出第一个单词定为最长单词.
while((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z'))
{
p++;l1++;
}
while(*p!='\0')
{
l=0;
while(!(*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z'))
p++;
q=p;
while((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z'))
{
p++;l++;
}
if(l>l1)
{
l1=l;max=q;
}
}
while((*max>='a'&&*p<='z')||(*max>='A'&&*p<='Z'))
{
putchar(*max);
max++;
}
}

[此贴子已经被作者于2006-4-1 21:38:22编辑过]


2006-04-01 14:24
快速回复:如何将一行字符串中最长的单词输出
数据加载中...
 
   



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

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