基础类知识(要求严格的字符串倒退输出),求最优解法

yujunliang316 2008-05-05 12:57:10
本人很懒,积分很少
..........................
要求是将一个字符串倒序输出例如字符串“i love you”,输出为“you love i”
字符长度不限,并且字符保存在char类型变量里面。
用标准C++的基础知识回答,不能使用vector、string等高级方法
........................
希望高人给予回答,谢谢。
...全文
360 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
yujunliang316 2008-05-06
  • 打赏
  • 举报
回复
分分了已经,感谢帮助
systemthink 2008-05-06
  • 打赏
  • 举报
回复
犯低级错误,自觉惭愧。。见下

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

#define MAX 100
=================
18楼的仍有点点问题
     最好不要c风格C++风格头文件一起用
有时会出莫名的问题
ciitc2 2008-05-06
  • 打赏
  • 举报
回复
好技术库,不容错过:http://www.ciitc.com
chendengbiao 2008-05-05
  • 打赏
  • 举报
回复
水平有限,可能有不妥的地方,望指教
其实我觉得要是真的实现的比较完善的话,可以用栈
#include<iostream.h>
#include<string.h>
struct Word
{
char a[20];
};
void main()
{
int i;
Word word_b[20];
char ch[]="i love you";
char *b;
b=new char[20];
int k=0,m=-1;
int j=strlen(ch);
for(i=0;i<j;i++)
{
if(ch[i]==' ')
{
b[m+1]='\0';
strcpy(word_b[k].a,b);
delete []b;
b=NULL;
b=new char[20];
m=-1;
k++;
}
else if(ch[i]!=' ')
{
m++;
b[m]=ch[i];
}
}
b[m+1]='\0';
strcpy(word_b[k].a,b);
for(i=k;i>=0;i--)
cout<<word_b[i].a<<" ";
}
happy446 2008-05-05
  • 打赏
  • 举报
回复
说实话 我看懂啥意思

怎么我看来这么简单的东西竟然能写这么多 愚钝啊我
stephenxu111 2008-05-05
  • 打赏
  • 举报
回复
要最高效率,可以
1)重写一个打印函数,以'\0'和空格作为结束符(默认的printf等函数以'\0'为结束符)
2)从字符串最后一个字符向前遍历,如果遇到空格,就用1)的函数输出其后的字符串

这样,只用从后往前扫描一遍即可,另外也不用再申请临时空间。
yujunliang316 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 cyr12 的回复:]
犯低级错误,自觉惭愧。。见下

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

#define MAX 100

[/Quote]
你还没有搞清楚那个要求呢......不过仍然感谢。
zywhuiss 2008-05-05
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int GetTokenNumber(char* ch,char token);
void GetStrs(char* source, char** array,int spaceNum,char token);

int main()
{
char* ch = "I/am/octopus/from/whu/iss/0303/!";//"I Love You !";
char token = '/';
cout<<"ch address: "<<(void*)ch<<endl;
//得到空格数量,并分配相应数量的指针数组
int PointNumber = GetTokenNumber(ch,token);
if(PointNumber!=0)
{
char** ch_arr = new char*[PointNumber];
GetStrs(ch,ch_arr,PointNumber,'/');
for(int i=PointNumber-1; i>=0; i--)
//for(int i=0; i<PointNumber; i++)
{
cout<<ch_arr[i]<<endl;
}
}
return 0;
}

void GetStrs(char* source, char** array,int spaceNum,char token)
{

if(source!=NULL && array!=NULL)
{
char* temp = source;
int i = 0;
int j;
char* ch;
int pos = 0;
int first = 0;
while(*temp!='\0' && i<spaceNum)
{
if(*temp==token)
{
ch = new char[pos-first+1];
for(j=0; j<pos-first; j++)
{
ch[j] = source[first+j];
}
ch[pos-first] = '\0';
first = pos + 1;
array[i] = ch;
temp++;
pos ++;
i++;
}
else
{
temp++;
pos ++;
}
}
//将最后一个字符串读取近来
ch = new char[pos-first+1];
for(j=0; j<pos-first; j++)
{
ch[j] = source[first+j];
}
ch[pos-first] = '\0';
array[i] = ch;
}
}

int GetTokenNumber(char* ch,char token)
{
/*
* 得到空格数量,用于分配指针数组
*/
if(ch!=NULL)
{
int spaceCount = 0;
char* temp = ch;
while(*temp!='\0')
{
if(*temp==token)
{
spaceCount ++;
}
temp++;
}
return ++spaceCount;
}
else
{
return 0;
}
}
任意指定一个token,给定一个以token分割的字符串,就可以达到楼上说的效果了.
dahua010 2008-05-05
  • 打赏
  • 举报
回复

#include <string.h>
#include <stdio.h>

char string[] = "i love you";
char seps[] = " ";
bool first = true;
char str[100];
void lsjprint()
{
char *token;

if(first)
{
first = false;
token = strtok( string, seps );
}
else
token = strtok( NULL, seps );
if(token == NULL)
return;
lsjprint();

strcat(str, token);
strcat(str, " ");
}
void main( void )
{
lsjprint();
printf( "%s\n", str );
}



测试通过,可以么....
cyr12 2008-05-05
  • 打赏
  • 举报
回复
犯低级错误,自觉惭愧。。见下

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

#define MAX 100

int main()
{
char csort[MAX], ch;
int i, j;
cout < <"输入一字符串" < <endl;
cin>>csort;
j=strlen(csort);

for(i=0; i <--j; i++)
{
ch = csort[i];
csort[i]= csort[j];
csort[j] = ch;
}
cout < <"转换后" < <csort < <endl;

return 0;
}
chowming 2008-05-05
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

int main()
{
const char *src="i love you";
char *des= new char[strlen(src)+1];
int start=strlen(src),end=strlen(src),count=0;
while(true)
{
while(src[--start]==' ')
end--;
while(start!=0&&src[--start]!=' ');
for(int i=start+1;i<end;i++)
des[count++]=src[i];
if(start==0)
break;
des[count++]=' ';
end=start;

}
des[count++]=src[0];
des[count]=0;
cout<<des<<endl;
return 0;
glchen57 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yujunliang316 的回复:]
引用 13 楼 glchen57 的回复:
char* pstr = "i love you";
char* pend = pstr+strlen(pstr);
char* pbeg = pend;
size_t cur = 0;
char* ptmp = new char[strlen(pstr)+1];
while(pbeg >= pstr)
{
if((*pbeg)!=' ' && (*pbeg)!='\t')
{
--pbeg;
continue;
}
memcpy(ptmp+cur, pbeg+1, pend-pbeg);
cur += pend-pbeg-1;
*(ptmp+cur) = *pbeg;
cur += 1;

pend = pbeg;
--pb…
[/Quote]

8楼用递归,而且不保存结果,所以看起来很简直。如果你要保存结果,就得在耗费一些空间代价。至于时间上的代价,跌代比递归会好一些。
yujunliang316 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 glchen57 的回复:]
char* pstr = "i love you";
char* pend = pstr+strlen(pstr);
char* pbeg = pend;
size_t cur = 0;
char* ptmp = new char[strlen(pstr)+1];
while(pbeg >= pstr)
{
if((*pbeg)!=' ' && (*pbeg)!='\t')
{
--pbeg;
continue;
}
memcpy(ptmp+cur, pbeg+1, pend-pbeg);
cur += pend-pbeg-1;
*(ptmp+cur) = *pbeg;
cur += 1;

pend = pbeg;
--pbeg;
}

if(pend > pbeg)
{
memcpy(ptmp+…
[/Quote]



很感谢,你能回帖,并且你的方法虽然有BUG也实现了功能。但是可以看到,你的方法相比8楼要差点。而且消耗资源要多
(个人心理猛的一看你的code很害怕)。10分奉上
yujunliang316 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 cyr12 的回复:]

试试看,我没调试过,应该没问题

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

#define MAX 100

int main()
{
char csort[MAX], ch;
int i, j;
cout < <"输入一字符串" < <endl;
cin>>csort;
j=strlen(csort);
//cout < <"转换前" < <csort < <endl;

for(i=0; i <j; i++,j--)
{
ch = csort[i];
csort[i]= cs…
[/Quote]

没有输出。
glchen57 2008-05-05
  • 打赏
  • 举报
回复
char* pstr = "i love you";
char* pend = pstr+strlen(pstr);
char* pbeg = pend;
size_t cur = 0;
char* ptmp = new char[strlen(pstr)+1];
while(pbeg >= pstr)
{
if((*pbeg)!=' ' && (*pbeg)!='\t')
{
--pbeg;
continue;
}
memcpy(ptmp+cur, pbeg+1, pend-pbeg);
cur += pend-pbeg-1;
*(ptmp+cur) = *pbeg;
cur += 1;

pend = pbeg;
--pbeg;
}

if(pend > pbeg)
{
memcpy(ptmp+cur, pbeg+1, pend-pbeg);
}

cout << ptmp << endl;
delete [] ptmp;
ptmp = NULL;

老实说,我不喜欢自己写的这个,觉得有更好的。
cyr12 2008-05-05
  • 打赏
  • 举报
回复

试试看,我没调试过,应该没问题

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

#define MAX 100

int main()
{
char csort[MAX], ch;
int i, j;
cout<<"输入一字符串"<<endl;
cin>>csort;
j=strlen(csort);
//cout<<"转换前"<<csort<<endl;

for(i=0; i<j; i++,j--)
{
ch = csort[i];
csort[i]= csort[j];
csort[j] = ch;
}
cout<<"转换后"<<csort<<endl;

return 0;
}

yujunliang316 2008-05-05
  • 打赏
  • 举报
回复
在等待1天结贴
yujunliang316 2008-05-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 akirya 的回复:]
C/C++ code

#include<stdio.h>
void func(char*p)
{
char *t = p;
while( (*t!=0 )&&(*t !=' ') )
t++;
if( *t !=0 ){
*t = 0;
func( t+1 );
}
printf("%s ",p);
}
int main()
{
char ch[]="i love you";
func( ch );
return 0;
}
[/Quote]


答案正确,该代码能正确实现所要求功能能,没有使用高级的东西,谢谢。80分送上
PcrazyC 2008-05-05
  • 打赏
  • 举报
回复
这个题的算法和将一个字符串反序输出有区别吗?
  • 打赏
  • 举报
回复


#include<stdio.h>
void func(char*p)
{
char *t = p;
while( (*t!=0 )&&(*t !=' ') )
t++;
if( *t !=0 ){
*t = 0;
func( t+1 );
}
printf("%s ",p);
}
int main()
{
char ch[]="i love you";
func( ch );
return 0;
}

加载更多回复(7)

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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