用c编写 : 输入一个字符串,找出字符串中最长的单词。

haokk 2001-08-20 05:50:28
例如输入 : i am a student. 则输出 :student


...全文
1221 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ychao 2001-08-21
  • 打赏
  • 举报
回复
# include <stdio.h>
# include <string.h>

void devide(char[],char[][15],int*);
int max(char[][15],int);

int main()
{
int wordNum=0,i=0,m;
char tgt[100],word[10][15]={' '};
printf("Insert the sentence please:\n");
gets(tgt);
printf("The sentence you input is:\n"); puts(tgt);

devide(tgt,word,&wordNum);
printf("The longest word is:\t");
m=max(word,wordNum);
while(word[m][i]!='\0'){
printf("%c",word[m][i]);
i++;
}
return 0;
}
void devide(char t[],char w[][15],int* wN)
{
int i=0,x=0,y=0;
while(t[i]!='\0'){
if(t[i]!=' '){
w[x][y]=t[i];
y++;
i++;
}

else{
i++;
x++;
y=0;
}
}
* wN=x+1;
}

int max(char t[][15],int num)
{
int x=0,n=strlen(t[0]),max=0;
while(x<num){
if(strlen(t[x])>n){
n=strlen(t[x]);
max=x;
}
x++;
}
return max;
}
mychome 2001-08-21
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "string.h"
#define R 20
#define C 30
void main()
{
char s[C][R],temp[R];
int i=0,j=0,t;
while((s[i][j]=getch())!='\n')
{
if(s[i][j]==' ')
{i++;
j=0;
}
else
j++;
}
for(t=0;t<i+1;t++)
puts(s+t);
for(j=0;j<i;j++)
for(t=j;t<i;t++)
if(strlen(s+t)<strlen(s+t+1))/*测字符长度*/
{
strcpy(temp,s+t);
strcpy(s+t,s+t+1);/*拷备*/
strcpy(s+t+1,temp);
}
for(t=0;t<1;/*字符最长的前几个*/t++)
puts(s+t);
}
我也是个初学者,这个程序是我才写的我也没试过,也不知行不,你试试,咱多交流好吗?我的Email:mychome@263.net
mychome 2001-08-21
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "string.h"
#define R 20
#define C 30
void main()
{
char s[C][R],temp[R];
int i=0,j=0,t;
wihile((s[i][j]=getch())!='\n')
{
if(s[i][j]==' ')
{
i++;
j=0;
}
else
j++;
}
for(t=0;t<i+1;t++)
puts(s+t);
for(j=0;j<i;j++)
for(t=j;t<i;t++)
{
if(strlen(s+t)<strlen(s+t+1))/*strlen(s)测字符长度*/
{strcpy(temp,s+t);/*s+t拷备temp*/
strcpy(s+t,s+t+1);
strcpy(s+t+1,temp);
}
printf("long is:");
for(t=0;t<1/*最长的前几个*/;t++)
puts(s+t);
}
我也是个初学者,这个程序我也没试过,你可以试试。咱多多交流,我的Email是:mychome@263.net
SCUM 2001-08-20
  • 打赏
  • 举报
回复

分词

排序





SPT 2001-08-20
  • 打赏
  • 举报
回复
能自己解决就自己解决
darcy_loren 2001-08-20
  • 打赏
  • 举报
回复
自己搞嘛!
这儿不是课堂呀!
书上有嘛!
liegou911 2001-08-20
  • 打赏
  • 举报
回复
书上有
reinly 2001-08-20
  • 打赏
  • 举报
回复
char *str[3]={'I','am','a','student'};
再比较数组中的长度,打印出最长的字符串
xiaoshitou 2001-08-20
  • 打赏
  • 举报
回复
我想用数组和指针,用空格分隔单词,可以找出两空格之间的字符数判断最长的单词
书上该有这个例子的,
dot99 2001-08-20
  • 打赏
  • 举报
回复
creat a arry to save the length of each word in this words
compare each length an chose the ID of the arry element
print the word
forwardforward 2001-08-20
  • 打赏
  • 举报
回复
根据空格取出单词;放在一个字符串数组中;求出最长的单词。
谭浩强C程序设计中有类似的习题,可以看解答。
xingworld 2001-08-20
  • 打赏
  • 举报
回复
gz
icu 2001-08-20
  • 打赏
  • 举报
回复
高程书上有例子,是个经典的算法
little_f 2001-08-20
  • 打赏
  • 举报
回复
看看你自己的C语言教程,自己解决,如果有问题,把你的源程序贴出,大家帮你解决问题

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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