将一行字符中最长的单词输出

napoleonpan 2003-08-11 12:23:53
//中间出错:lvalue required.
#include<iostream.h>
#include<stdio.h>
char f1(char b[20]){
int u,i,k=0,t=0;
char c[20]={0};//c函数用来记录最长的那个单词
for(i=0;i<=19;i++){
if(b[i]=='\0')break;
else{
if(b[i]==' '){//遇到空格,统计空格前面的那个单词
if(k>t){
t=k;k=0;//如果此单词比前面的单词都长,
//则用t记录此单词长度。
for(u=0;u<=t;u++){//用c函数记录空格前
//面的单词
c[u]=b[(i-t)++];//出错行:显示lvalue
} //required.
}
}
else k++;//统计每个单词的长度用k
}
}
cout<<c;
}
void main(){
char a[20];
gets(a);
f1(a);
}
...全文
160 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fupa0 2003-08-11
  • 打赏
  • 举报
回复
更正一下

(i-t)不是左值,无法直接改变其值
fupa0 2003-08-11
  • 打赏
  • 举报
回复
(i-t)是一个常量
++操作符不对常量进行操作
积木 2003-08-11
  • 打赏
  • 举报
回复
拿STL写多好

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string key = "\ \t";
//这里的'\ '是一个空格,
string str = "googdoy is me";
string::iterator word_begin = str.begin();
string::iterator word_end = str.begin();
int max_length = 0;
string::iterator max_iter = str.begin();
while(word_end != str.end())
{
word_end = find_first_of(word_begin,str.end(),key.begin(),key.end());
if(word_end - word_begin > max_length)
{
max_length = word_end - word_begin;
max_iter = word_begin;
}
word_begin = word_end + 1;
}
cout<<"最大的单词:"<<string(max_iter,max_iter+max_length);
return 0;
}
shaneylee 2003-08-11
  • 打赏
  • 举报
回复
#define NULL 0;
main()
{
char s,*p,Word[50];
int i=0,j=0,k,WordLength[50],MaxWordLength=1;
printf("The string you input is:\n");
scanf("%s",p);
while(*p!=NULL)
{
if(*p!=' ')
{
Word[i]=*p;
i++;
}
else {
WordLength[j]=i+1;
j++;
}
p++;
}
for(k=0,k<=j,k++)
if(WordLength[k]>=MaxWordLength)
MaxWordLength=WordLength[k];
printf("The longest word of your sentence is:\n");
printf("%d",MaxWordLength);
}
silinxu 2003-08-11
  • 打赏
  • 举报
回复
void main()
{
char string[1000];
int i,num=0,word=0;
char c;
static int a[1000];
static int b[1000];
int max;
int k=0;;
int flag=0;
gets(string);
for(i=0;(c=string[i])!=0;i++)
{
if(c==' ')
word=0;
else if(word==0)
{
word=1;
num++;
flag=0;
}

if(word==1)
{
a[num-1]++;
if(flag==0)
b[num-1]=i;
flag++;
}

}
max=a[0];
for(i=0;i<num;i++)
if(a[i]>max)
{
max=a[i];
k=b[i];
}

for(i=k;i<k+max;i++)
printf("%c",string[i]);
}



这个怎么样??
binglingbanglang 2003-08-11
  • 打赏
  • 举报
回复
我发现我这个程序有问题,
如果输入aa bbb cccc 这就输出cccc ,没错
cccc bbb aa最后没有空格结束时输出cccc,都没错,但:
cccc bbb aa最后有空格结束时就输出bb aa
请问为什么会这样的?
luhongming 2003-08-11
  • 打赏
  • 举报
回复
写程序要有好的书写习惯,格式很重要。
binglingbanglang 2003-08-11
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<stdio.h>
char f1(char b[20]){
int u,i,k=0,t=0,g=0;
char c[20]={0};
for(i=0;i<=19;i++){
if(b[i]=='\0')break;
else{
if(b[i]==' '){
if(k>t){
t=k;k=0;g=(i-t);
for(u=0;u<=t;u++){
c[u]=b[g++];
}
}
}
else k++;
}
}
cout<<c;
return 0;
}
void main(){
char a[20];
gets(a);
f1(a);
}
这样就没有问题了,但输入最后一个单词时要打多一个空格
ppm07 2003-08-11
  • 打赏
  • 举报
回复
左值就是能写在等号左边的,也就是能别赋值的.
(i-t)能被赋值吗?
如果能被赋值的话,起赋值结果保存在哪里呢?难道要为(i-t)再开辟一个空间?不可能的嘛.
xueweizhong 2003-08-11
  • 打赏
  • 举报
回复
STL-way:
#include <ctype.h>
#include <algorithm>

namespace __detail
{
inline
void skip_space(char const*& str)
{
while (isspace(*str)) ++str;
}

inline
void skip_not_space(char const*& str)
{
while (!isspace(*str) && *str) ++str;
}
}

struct MyIter
{
char const* begin;
char const* end;
MyIter(char* const str)
{
forward(str);
}

MyIter& operator ++ ()
{
forward(end);
return *this;
}

int operator *()
{
return end-begin;
}

bool operator == (MyIter const& other)
{
return *begin == 0 && *other.begin == 0;
}

bool operator != (MyIter const& other)
{
return !(*this == other);
}
private:
void forward(char const* str)
{
begin = str;
__detail::skip_space(begin);
end = begin;
__detail::skip_not_space(end);
}

};

int main()
{
MyIter begin("1 23 4");
MyIter end("");

MyIter bingo = std::max_element(begin, end);
}

/////////////////////////////////////////////////////////

non-stl-way:

#include <ctype.h>
#include <utility>
#include <stddef.h>

using std::pair;

inline
void skip_space(char const*& str)
{
while (isspace(*str)) ++str;
}

inline
void skip_not_space(char const*& str)
{
while (!isspace(*str) && *str) ++str;
}

pair<int, char const*>
max_len_part(char const* str)
{
pair<ptrdiff_t, char const*> result;

do
{
skip_space(str);
char const* begin = str;
skip_not_space(str);
if (str-begin > result.first)
{
result.first = str-begin;
result.second = begin;
}
} while (*str);

return result;
}

int main()
{
char const* p = max_len_part("1 23 4333 23").second;
}

napoleonpan 2003-08-11
  • 打赏
  • 举报
回复
能具体说一下吗,什么是左值
另外,i-t应该是变量

69,336

社区成员

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

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