how are you 转换为 you are how

lianlove 2009-04-28 08:48:08
如何将how are you 转化为 you are how ,不用c++以及c库函数

char * a=“how are you”;
实现 char *b=“you are how”
...全文
1341 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
brightxu 2009-05-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhangwuji155 的回复:]
请原谅我用如此粗糙之手法,仅为完成作业用,没有注释,
如果算法不打算自己做,而且懒得自己研究别人的算法,劝你还是别学编程了。
[/Quote]
严重同意!
lianlove 2009-04-30
  • 打赏
  • 举报
回复
my5123的方法很好,比较好管理,而且可扩展性很强!除了申请的内存没有delete外,其他的都非常完美!自己的问题自己也要解决,先将真个字符串倒序,再将每个单词倒序,程序如下:

void reverseString(char * src,int startIndex ,int endIndex)
{
for(int i=startIndex,j=endIndex;i<j;i++,j--)
{
char temp=*(src+i);
*(src+i)=*(src+j);
*(src+j)=temp;
}
}

int main(int argc,char *argv[])
{
char * source="the problem should be resolve by myself";
char *temp;
temp=source;
int count=0;
int start=0;
int end=0;
int index;
while(*(temp++)!='\0')
count++;
index=count-1; // don't include the end char '\0'
reverceString(source,0,index);// reverce the whole string
temp=source;

while(1)
{

if(*(temp+end)==' ')
{
reverceString(source,start,end-1);
start=end+1; //start points the first char of the word needing to deal
}
if(*(temp+end)=='\0')
{
reverceString(source,start,end-1);
break;
}
++end;
}
}




lianlove 2009-04-29
  • 打赏
  • 举报
回复
不是我没研究,当时是笔试,没多少时间仔细想想,我也是将每个单词存到数组里面,然后在从最后的数组依次往前连接,后来回来又由一个想法,就是先将char*依次从最后一个字符取到第一个字符,然后在将每个单词的最后一个跟第一个对调,单词的第二个字符跟单词的倒数第二个对调,依次类推,这种方法貌似比较容易实现。期待高手有更好的解决办法。今天太累了,明天好好看看楼上的算法,合适的法我也会给分的。
kusey 2009-04-29
  • 打赏
  • 举报
回复
my5123 代码内存泄漏。
my5123 2009-04-29
  • 打赏
  • 举报
回复
用了单向链表,没有什么局限性,期待高手更好的答案
my5123 2009-04-29
  • 打赏
  • 举报
回复
typedef struct ST_REVERSE
{
ST_REVERSE* pPre;
char *psWord;
int nCount;
}*PST_REVERSE;

int _tmain(int argc, _TCHAR* argv[])
{
int nIndex = 0;
int nCharacterInWordCount = 0;
char *a = "Why is that our corporation won`t give us half a day off on May 4th!!";
char *pszFinal;
PST_REVERSE pstWord = NULL;
PST_REVERSE pstWordPre = NULL;

//将原字符串中每个单词首字母的指针和单词中的字母数放入单向链表
while (1)
{
if (*a == ' ' || *a == '\0')
{
pstWord = new ST_REVERSE;
pstWord->pPre = pstWordPre;
//首个单词前没有空格
if (pstWord->pPre != NULL)
{
--nCharacterInWordCount;
}
pstWord->psWord = a - nCharacterInWordCount;
pstWord->nCount = nCharacterInWordCount;
pstWordPre = pstWord;
nCharacterInWordCount = 0;
}
if (*a == '\0')
{
break;
}
++nIndex;
++nCharacterInWordCount;
++a;
}

//从单向链表的最后一项开始往前,将前一个单词拷入新字符串中
pszFinal = new char[nIndex + 1];
nIndex = 0;
while (1)
{
for (int i = 0; i != pstWord->nCount; ++i)
{
pszFinal[nIndex] = pstWord->psWord[i];
++nIndex;
}
pszFinal[nIndex] = ' ';
++nIndex;
pstWord = pstWord->pPre;
if (pstWord == NULL)
{
pszFinal[nIndex] = '\0';
break;
}
}

printf("%s\n", pszFinal);
return 0;
}
xghuzd 2009-04-29
  • 打赏
  • 举报
回复
你个思路:
先出一个个单词放到数组中,
数组倒序,
数组再接在一起.
zhangwuji155 2009-04-29
  • 打赏
  • 举报
回复
有点小错误,更正
#include <stdio.h>
//只为完成功能,都没有进行内存管理,自己处理
char * GetWord(const char* s, int &index)
{
char *g=new char[20];
int i;
for(i=0; s[i]!=0; i++)
{
if(s[i]==' ')
break;
g[i]=s[i];
}
g[i]=0;
index = i;
return g;
}
int MyMemCpy(char *goal, const char* s)
{
int ii;
for(ii=0; s[ii]!=0; ii++)
{
goal[ii]=s[ii];
}
return ii;
}
int main(int argc, char *argv[])
{
char *a = "how are you";
char *b = new char[50];
int i,j,index;
long Address[10];
for(i=0, j=0; a[i]!=0; i++)
{
Address[j]=(long)GetWord(a+i, index);
i+=index;
j++;
if(a[i]==0)
{
break;
}
}
int ii=0;
// j = j-1;
for(;j>0;j--)
{
i = MyMemCpy(b+ii, (char*)Address[j-1]);
b[ii+i]=' ';
ii = ii+i+1;
}
printf("%s", b);
scanf("%s", a);
}
zhangwuji155 2009-04-29
  • 打赏
  • 举报
回复
请原谅我用如此粗糙之手法,仅为完成作业用,没有注释,
如果算法不打算自己做,而且懒得自己研究别人的算法,劝你还是别学编程了。
_飞翔的企鹅_ 2009-04-29
  • 打赏
  • 举报
回复
这是什么玩意
zhangwuji155 2009-04-29
  • 打赏
  • 举报
回复
#include <stdio.h>
//只为完成功能,都没有进行内存管理,自己处理
char * GetWord(const char* s, int &index)
{
char *g=new char[20];
int i;
for(i=0; s[i]!=0; i++)
{
if(s[i]==' ')
break;
g[i]=s[i];
}
g[i]=0;
index = i;
return g;
}
int MyMemCpy(char *goal, const char* s)
{
int ii;
for(ii=0; s[ii]!=0; ii++)
{
goal[ii]=s[ii];
}
return ii;
}
int main(int argc, char *argv[])
{
char *a = "how are you";
char *b = new char[50];
int i,j,index;
long Address[10];
for(i=0, j=0; a[i]!=0; i++)
{
Address[j]=(long)GetWord(a+i, index);
i+=index;
j++;
}
int ii=0;
j--;
for(;j>0;j--)
{
i = MyMemCpy(b+ii, (char*)Address[j-1]);
b[ii+i]=' ';
ii = ii+i+1;
}
printf("%s", b);
}
zinking3 2009-04-29
  • 打赏
  • 举报
回复
发错地方了,小朋友

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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