C 如何实现字符串倒序

guanlicome 2008-12-08 10:00:00
我是做java的,面试时被问到这个问题,我回答1.用字符数组,倒序排列。2.c++stl
面试官说你的解决方法还是类似java的。

向大家请教。分不够再加。
...全文
1363 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
mifeixq 2008-12-08
  • 打赏
  • 举报
回复
恩,随便写了一个,用到了临时空间,算法不是很好的程序,不过这个问题很简单,用点临时空间也没有问题

程序如下

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

int main(int argc, char * argv[])
{
if(argc != 2)
{
printf("Usage: ./a.out string\n");
return -1;
}

int len = strlen(argv[1]);
int i=0;
char *p = malloc(sizeof(char)*(len + 1));

for(;i<len;++i)
p[i] = *(argv[1] + len-i-1);
p[i] = 0;

printf("%s\n", p);
free(p);
return 0;
}

结果
mifei@P-I-mi:~/test$ ./a.out
Usage: ./a.out string
mifei@P-I-mi:~/test$ ./a.out 12345
54321
nullah 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 WizardOz 的回复:]
一个循环就搞定,干嘛是还用复杂的数据结构啊?


C/C++ code

l = strlen(str);

for(int i = 0;i<l/2;++i){
swap(str+i,str+l-i);
}
[/Quote]
hehe
xyxwangkai 2008-12-08
  • 打赏
  • 举报
回复
反转的话,只需开辟一个字节的空间,然后前后调换,思路很清晰的
qap22 2008-12-08
  • 打赏
  • 举报
回复
帮顶
晴风smile 2008-12-08
  • 打赏
  • 举报
回复
学习了...huhu
WizardOz 2008-12-08
  • 打赏
  • 举报
回复
到底是反转还是排序阿?
要是排序的话,正序倒序不一样吗?干嘛要强调说“倒序”阿?
WizardOz 2008-12-08
  • 打赏
  • 举报
回复
一个循环就搞定,干嘛是还用复杂的数据结构啊?



l = strlen(str);

for(int i = 0;i<l/2;++i){
swap(str+i,str+l-i);
}

nullah 2008-12-08
  • 打赏
  • 举报
回复

void reverse(char *p)
{
if(*p = '\0')
{
}
else
{
reverse(p+1);
cout << *p;
}
}
iunion 2008-12-08
  • 打赏
  • 举报
回复
还是用2分法

void bsort(char *cp,int count)
{
int i,j,low,mid,high;
char t;
for(i=1;i<count;i++)
{
t=cp[i];
low=0;
high=i-1;
while(low<=high)
{
mid=(low+high)/2;
if(t<cp[mid])
high=mid-1;
else
low=mid+1;
}
for(j=i-1;j>=low;j--)
cp[j+1]=cp[j];
cp[low]=t;
}
return;
}

这是正序,倒序自己改吧,想省事就在调用一次我上面写的字符串反转
huangpeng8612 2008-12-08
  • 打赏
  • 举报
回复
呵呵 我也刚看明白是要排序.那就用头文件altorithm中的sort函数.原型如下:
#include <algorithm>
void sort( iterator start, iterator end );
void sort( iterator start, iterator end, StrictWeakOrdering cmp );

第一个只需要传递你要排序的串(整形数组等都行)的头指针(数组第一个元素的指针)与数组最后元素的下一个位置,sort是一个模板哈

第二个前面两个参数同第一,但第三个参数是传递一个你定义用于排序的函数(返回比较的大小值,如strcmp或自定义的都行).
你写一个比较大小的函数就行了,将大小反过来返回.
huangpeng8612 2008-12-08
  • 打赏
  • 举报
回复
如果不用汇编,巧妙的方法就是C++中的STL了.algorithm文件中有一个函数专门实现倒序的,原型如下:

template<class BidirectionalIterator>
void reverse(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
iunion 2008-12-08
  • 打赏
  • 举报
回复
排序呀,又看错题了,以上作废
aaajj 2008-12-08
  • 打赏
  • 举报
回复
用指针实现,思路还是很清晰的
iunion 2008-12-08
  • 打赏
  • 举报
回复

简单写了一下
void swapstr(char* str)
{
int strl, i;
char p;

strl = strlen(str);

for (i=0; i<strl/2; i++)
{
p = *(str+i);
*(str+i) = *(str+strl-i-1);
*(str+strl-i-1) = p;
}
}

huangpeng8612 2008-12-08
  • 打赏
  • 举报
回复
没必要真把内容倒过来.如果C中加点低级的汇编,你会发现完全没有这个必要. CPU中都有一个字符串方向标志,设为从低到高的指令为CLD, 设为从高地址到低地址的指令为STD.一般都是从低到高,只要把这个方向标志位反过来就可以了.
zhw888888 2008-12-08
  • 打赏
  • 举报
回复
#include<stdlib.h>
#include<stdio.h>
char String[30];
int Length;
void Reverse(int n)
{
if(N<length)
{
Reverse[N+1];//递归执行部分
printf("%c",String[N]);
}
}
void main()
{
printf("please enter string:");
scanf("%s",&String);//输入原始字符
Length=strlen(String);//取得字符串长度
printf("The reverse string:");
Reverse(0);//调用递归函数
printf("\n");
}
nullah 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 cyj626 的回复:]
c就要用指针了 ,
可以递归啊
[/Quote]
同意4楼
whsfer 2008-12-08
  • 打赏
  • 举报
回复
我这里有一个关于链表反转的,虽说不是字符串反转,但也是可以看一下的
void ReverseList(LinkList& list)
{
LinkList pre,cur,ne;
pre=list;
cur=list->next;
while(cur)
{
ne=cur->next;
cur->next=pre;
pre=cur;
cur=ne;
cout<<pre->data<<cur->data<<ne->data<<endl;
}
list->next=NULL;
list=pre;
}
cyj626 2008-12-08
  • 打赏
  • 举报
回复
c就要用指针了 ,
可以递归啊
candyice 2008-12-08
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20030613/02/1909885.html看看这个吧
加载更多回复(2)

69,364

社区成员

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

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