请问:一道编程题

goodsun1 2005-10-25 10:41:12
颠倒一个句子中的词的顺序,比如将"my name is vick"转换为"vick is name my",要求占用空间最少。
大家有什么好的想法啊
...全文
315 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
sharc 2005-10-26
  • 打赏
  • 举报
回复
找出中间位置,1和n交换,2和n-1,........
给个函数实现吧!
void strdev1(char* string)
{
int number = 0; // 字符个数
int i,j;
char* temp; // 用于遍历的指针
temp = string;

// 取出字符个数
while(*(temp++) != '\0' )
{
++number;
}
// 交换有效字符
temp = string;
for(i=number-1,j =0;i>=(int)(number/2);i--,j++)//i指向串尾,j指向串头
{
char tempchar;
tempchar = *(temp + i);
*(temp + i) = *(temp + j);
*(temp + j) = tempchar;
}

}
逸学堂 2005-10-26
  • 打赏
  • 举报
回复
反向读出,不就行了!
呵呵
还是用模板库吧
有现成的函数,效率应该比我们写的要高!
kokoshan 2005-10-26
  • 打赏
  • 举报
回复
我想思路应该是这样:
先分析找出每个单词,然后放入数组中,然后倒序数组就应该OK了吧
sankt 2005-10-26
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<algorithm>


using namespace std;

int main()
{

string a[]={"my","name","is","nike"};
int i;
size_t size=sizeof(a)/sizeof(a[0]);
cout<<size<<endl;
ostream_iterator<string> out(cout," ");
copy(&a[0],&a[size],out);
cout<<endl;

string temp;

for(i=0;i<size/2;++i)
{
temp=a[i];
a[i]=a[size-i-1];
a[size-i-1]=temp;
}

copy(&a[0],&a[size],out);
cout<<endl;


return 0;
}
wangsheng1984 2005-10-26
  • 打赏
  • 举报
回复
用栈实现吧
先将它入栈,然后定义两个指针,开始做POP操作,指针从K开始检查,如果内容为空,就POP出走过的一段,这时把这个位置用另一个指针记录,重复操作就可以了
chenxinzai 2005-10-26
  • 打赏
  • 举报
回复
#include <string.h>
void main()
{

char s[] = "my name is vick";
char q[10][10];
char *delim = " ";
char *p;
int i = 0,j = 0;
strcpy(q[i],strtok(s,delim));/*strok函数切割字符串*/
while(p = strtok(NULL,delim)){

strcpy(q[++i],p);
}

for(i;i>=0;i--)
{ if(i>0)
printf("%s ",q[i]);
else
printf("%s\n" ,q[i]);
}
}
newzai 2005-10-26
  • 打赏
  • 举报
回复
//修正
采用递归算法.函数名称为 ReverseWord(char *,int len)//len是字符串的长度,不是单词的长度.
1.统计单词的个数.一般是以空格为基准,空格数=单词数-1
循环遍历给定的字符串,查找空格,统计空格blank_count
2.
if blank_count =0 //只有一个单词
return ;
if blank_count = 1
{
//只有两个单词,,还不简单吗..
调换两个单词
return
}else {
//两个递归调用.
ReverseWord(第(bland_count+1)/2个空格以前的单词(str1),长度);
ReverseWord(第(bland_count+1)/2个空格以后的单词(str2),长度);
调换 str1与str2
return

}

newzai 2005-10-26
  • 打赏
  • 举报
回复
采用递归算法.函数名称为 ReverseWord(char *,int len)//len是字符串的长度,不是单词的长度.
1.统计单词的个数.一般是以空格为基准,空格数=单词数-1
循环遍历给定的字符串,查找空格,统计空格blank_count
2.
if blank_count = 1
{
//只有两个单词,,还不简单吗..
调换两个单词
return
}else if bland_count 是偶数 {
//两个递归调用.
ReverseWord(第bland_count/2个空格以前的单词(str1),长度);
ReverseWord(第bland_count/2 +1 个空格以后的单词(str2),长度);
调换 str1与str2
return

}else //奇数
{
ReverseWord(第bland_count/2+1个空格以前的单词(str1),长度);
ReverseWord(第bland_count/2 +1 个空格以后的单词(str2),长度);
调换 str1与str2
return

}

tesling 2005-10-26
  • 打赏
  • 举报
回复
给个c++ stl的实现。单词以q结尾,表示结束输入。
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main(int argc, char* argv[])
{
vector<string> strVec;
string e;
while(true)
{
cin>>e;
if(e.compare("q")==0)
break;
strVec.push_back(e);
}
int front = 0;
int back = strVec.size()-1;
while(front<=back)
{
swap(strVec[front],strVec[back]);
front++;
back--;
}
ostream_iterator<string> output(cout," ");
copy(strVec.begin(),strVec.end(),output);
cout<<endl;
return 0;
}
xiaocai0001 2005-10-26
  • 打赏
  • 举报
回复
串中有标点符号不?
knightraincn 2005-10-26
  • 打赏
  • 举报
回复
要求空间最少的话
能不能先将整个字符串倒序,然后再将空格之间的单词倒序
lone_512 2005-10-26
  • 打赏
  • 举报
回复
用二维数组吧.
#include<stdio.h>
#define M 40
#define N 20
void main()
{
int c;
int n;
int j=0;
int nline=0;
int nword=0;
char s[M][N];
int numw[10];
while((c=getchar())!=EOF)
{
s[nline][nword]=c;
numw[j]=nword;
nword++;
if(c==' '||c=='\n')
{
nline++;
nword=0;
j++;
}
continue;

}
j=0;
for(n=nline-1;n>=0;n--)
{
for(nword=0;nword<numw[n];nword++)
printf("%c",s[n][nword]);
printf(" ");
}
}
miniplayer 2005-10-26
  • 打赏
  • 举报
回复
先用空格区分每个单词 然后反向排列 去网上找找 应该有现成函数的
cdo 2005-10-26
  • 打赏
  • 举报
回复
应该用空格来分开.
goodsun1 2005-10-26
  • 打赏
  • 举报
回复
里面没有标点,颠倒一句话中单词的位置。
snowmail 2005-10-26
  • 打赏
  • 举报
回复
sharc的好像不对啊。照这个方法就是把整个串颠倒序列了,楼主好像要求的是颠倒一句话中单词的位置。
qhfu 2005-10-25
  • 打赏
  • 举报
回复
第一个和最后一个,依次交换。

69,382

社区成员

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

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