输出一个句子中最长的单词

_-_-_-_- 2005-05-19 04:52:33
输出一个句子中最长的单词

就是输入一个字符串。。也就是句子吧。。。
之后输出 最长的单词。。。单词就是靠空格来判断。。。。

5555555555~~~~~~~~我一直学C#的。。。今天一个小弟来问我这个问题。。。要求用c语言做。。。
我说明天才告诉他。。。大家帮忙~~~~~~谢谢~~~~~~
...全文
764 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
_-_-_-_- 2005-05-19
  • 打赏
  • 举报
回复
谢谢~~
虽然我可以用C#写个简单的信息系统了。。但是面对这些问题。。。我居然做不出。。。没面子了。。。看来基础知识需要补补啊~~~~~
guofu_x 2005-05-19
  • 打赏
  • 举报
回复
已经有这么多程序了,不用我出手了
CPPLOVER_78 2005-05-19
  • 打赏
  • 举报
回复
#include <stdio.h>

void main()
{
//先获得要判断的句子
char str[500];
printf("请输入一个句子:");
gets(str);

//判断句子是否为空
if(strlen(str)==0)return;

//定义临时变量;
char* wordstart1=0;
char* wordend1=0;
char* wordstart2=0;
char* wordend2=0;


bool isWord=false;
char*p=str;
while(true)
{
if((*p)>='A' && (*p)<='z')
{
if(isWord==false)
{
wordstart1=p;
isWord=true;
}
}
else
{
if(isWord==true)
{
wordend1=p;
isWord=false;

if((wordend2-wordstart2)<(wordend1-wordstart1))
{
wordend2=wordend1;
wordstart2=wordstart1;

}
}
}
if(*p=='\0')break;
p++;
}


//将得到的值拷贝到新的内存中;
if(wordstart2>=wordend2)
{
printf("输入的句子中一个单词也没有!\n");
return;
}
char* maxWord=(char*)malloc(wordend2-wordstart2+1);
if(maxWord==NULL)exit(0);

memset(maxWord,0,wordend2-wordstart2+1);
memcpy(maxWord,wordstart2,wordend2-wordstart2);

printf("最长的单词是%s\n",maxWord);

free(maxWord);
maxWord=NULL;

}
wqxhome 2005-05-19
  • 打赏
  • 举报
回复
和楼上的思路一样
wqxhome 2005-05-19
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "conio.h"

main()
{
char a[100];
int i,mark,lenst,wlen,wstat,slen; /*声明变量*/
slen=99;mark=0;lenst=0;wlen=0;wstat=0; /*初始化变量*/

printf("input a sentence:"); /*输入句子*/
gets(a);

/*找出句子长度*/
for(i=0;i<99;i++)
if(a[i]=='\0')
{
slen=i;
break;
}


for(i=0;i<=slen;i++) /*找出最长单词起点和长度*/
if(a[i]==' '||a[i]=='\0')
{wlen=i-mark;
if(wlen>lenst)
{lenst=wlen;
wstat=mark;
}
mark=i+1;
}

printf("the longest word is:"); /*打印最长单词*/
for(i=wstat;i<wstat+lenst;i++)
printf("%c",a[i]);

}
copygirl 2005-05-19
  • 打赏
  • 举报
回复
遇到空格后就判断该字符串是不是比以前遇到过的所有字符串都长,如果是,就把它放到str2中。
最后打印出str2。
copygirl 2005-05-19
  • 打赏
  • 举报
回复
大概是这样的:
#include "stdio.h"

main()
{
char *str="kdleiejfa ieowjf ieuuowfjeowfj efaskf";
char *buf1,*buf2;
char aa[2];
int i;

for(i=0;i<strlen(str);i++)
{
if(str[i]!=32)
{
aa[0]=str[i];
aa[1]="\0";
strcat(buf1,aa);
}
else
{
if(strlen(buf1)>strlen(buf2))
buf2=buf1;
buf1=0;
}
}
printf("%s\n",buf2);
getch();
}
tfq 2005-05-19
  • 打赏
  • 举报
回复
注意一样长的可能有多个
yesiloveyou 2005-05-19
  • 打赏
  • 举报
回复
前天写的程序 以','分隔单词/

#include <iostream>
#include <string>
using namespace std;

const int MAX=10;

string input(string &s){ //输入String
cout<<"The String is:"<<endl;
cin>>s;
return s;
}

int find(string &s,int *a){ //找最长的,找度存数组a[]
int i=0;
int n=0;
int k=0;
while(s[i]!='\0'){
if(s[i]!=',')
k++;
else {
a[n]=k;
k=0;
n++;
}
i++;
}
return n;
}

int sMax(int a[],int n){ //排序..找出最长的值
int m=0;
for(int i=0;i<n;++i)
for(int j=i;j<n;++j){
int temp;
if(a[i]<a[j+1]){
temp=a[j+1];
a[j+1]=a[i];
a[i]=temp;
}
}
m=a[0];
return m;
}

int main(){
string s;
cout<<"\t\tThis String use \",\" separate."<<endl;
input(s);
int a[MAX];
int n;
n=find(s,a);
int max;
max=sMax(a,n);
cout<<"The count of max char:\n"<<max<<endl;
int e=0; //存最找元素在字符串中的位置
for(int i=0;i<n;++i){
if(a[0]=max)
e=0;
else if(a[i]!=max)
e+=a[i]+1;
}
cout<<"Char:"<<endl;
for(i=e;i<e+max;++i)//输出最找字符串
cout<<s[i];
cout<<endl;
system("pause");
return 0;
}

69,381

社区成员

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

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