⼀个实现字符串反转的函数⼀个实现字符串反转的函数
提供三种⽅法:
1、有中间变量
2、⽆中间变量
3、堆栈实现
/***************************************************
Name: reverse_str.c
Copyright: kernelxu
Author: kernelxu
Date: 29-08-05 12:10
Description: reverse astring allocated in an array
将⼀个在数组中的字符串倒置。
My E_mail:
***************************************************/
#include <stdio.h>
#include <string.h>
void reverse_str(char * ch);
void reverse_str2(char *ch);
int main(void)
{
char c[] = "Can you reverse me?";
printf("original string c: /n%s/n", c);
reverse_str(c);
printf("reversed string after calling reverse_str: /n%s/n", c);
reverse_str2(c);
printf("reversed string after calling reverse_str2: /n%s/n", c);
system("pause");
return 0;
}
void reverse_str(char *ch) /*使⽤中间变量*/
{
int len;
int i;
len = strlen(ch)-1;
char ctemp;
for(i = 0; i < len-i; i++)
{
ctemp = ch[i];
ch[i] = ch[len-i];
ch[len-i] = ctemp;
}
ch[len+1] = 0;
}
-------------------------------------------------------
void reverse_str2(char *ch) /*不⽤中间变量*/ {
int len;
int i;
len = strlen(ch)-1;
char ctemp;
for(i = 0; i < len-i; i++)
{
ch[i] = ch[i] ^ ch[len-i];
ch[len-i] = ch[i] ^ ch[len-i];
ch[i] = ch[i] ^ ch[len-i];
}
ch[len+1] = 0;
}
---------------------------------------------------------//堆栈实现
//reverse the string 's1' except the substring 'token'.
const char* reverse(const char* s1, const char* token)
{
assert(s1 && token);
stack<char> stack1;
const char* ptoken = token, *head = s1, *rear = s1;
while (*head != '/0')
{
while(*head!= '/0' && *ptoken == *head)
{
ptoken++;
head++;
}
if(*ptoken == '/0')//contain the token
{
const char* p;
for(p=head-1;p>=rear;p--)
stack1.push(*p);
ptoken = token;
rear = head;
}
else
{
stack1.push(*rear);
head=++rear;
ptoken = token;
}
}
char * return_v = new char[strlen(s1)+1];
int i=0;
while(!pty())
{
return_v[i++] = p();
stack1.pop();
}
return_v[i]='/0';
return return_v;
}
int main(int argc, char* argv[])
{
cout<<"This is zhuxinquan's Chinese site: www.zhuxinquan/cn/n";
cout<<reverse("This is zhuxinquan's Chinese site: www.zhuxinquan/cn","zhuxinquan"); return 0;
}
//reverse the string 's1' except the substring 'token'.
const char* reverse(const char* s1, const char* token)
{
assert(s1 && token);
stack<char> stack1;
const char* ptoken = token, *head = s1, *rear = s1;
while (*head != '/0')
{
while(*head!= '/0' && *ptoken == *head)
{
ptoken++;
head++;
}
if(*ptoken == '/0')//contain the token
{
const char* p;
for(p=head-1;p>=rear;p--)
stack1.push(*p);
ptoken = token;
rear = head;
}
else
{
stack1.push(*rear);
head=++rear;
ptoken = token;
}
}
char * return_v = new char[strlen(s1)+1];
int i=0;
while(!pty())
{
return_v[i++] = p();
stack1.pop();
}
return_v[i]='/0';
return return_v;
}
int main(int argc, char* argv[])
{
cout<<"This is zhuxinquan's Chinese site: www.zhuxinquan/cn/n";
cout<<reverse("This is zhuxinquan's Chinese site: www.zhuxinquan/cn","zhuxinquan"); return 0;
字符串比较函数实现}
转⾃:
-----------------------------------------------------------------
题⽬:
编写函数reverse_string,它的原型如下:
void reverse_string(char *str);
函数把参数字符串中的字符串反向排列。请使⽤指针⽽不是数组下标,不要使⽤任何C函数库中⽤于操纵字符串的函数。提⽰:不要声明⼀个局部数组来临时存储参数字符串。
算法:
关于反转字符串的⽅法很多。这⾥我给出我想到的⼀种算法。
/************反转字符串************/
void reverse_str(char*str)
{
char*cp=str;
int i=0;
while(*str!='/0')
{
str++;
i++;
}
i/=2;
str--;
while(i-->0)
{
*str^=*cp;
*cp^=*str;
*str^=*cp;
str--;
cp++;
}
}
说明:
算法中运⽤了:a ^ a = 0 和 a ^ 0 = a的特性。以此来进⾏变量值的交换,可以避免使⽤额外的临时变量。
转⾃:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论