您的位置:首页 > 其它

字符串处理基础(一)反转,循环移动

2014-04-25 23:39 141 查看
#pragma once
using namespace std;
void inverse(char *p);
void printString(char *p);
void rightMove(char *p,int n);
void inverse2(char *p);
void inverse(char *p)
{
if (p==NULL||*p=='\0')
{
return;
}
char *head=p;
char *last=p;
while((*last)!='\0')
{
last++;
}
last--;
char temp;
while(head<=last)
{

temp=*head;
*head=*last;
*last=temp;
head++;
last--;
}
}

void printString(char *p)
{
if (p==NULL)
{
return;
}
while(*p!='\0')
{
cout<<*p;
++p;
}
}
void rightMove(char *p,int n)
{
if (p==NULL||n<0)
{
return;
}
int len=0;
char *last=p;
while(*last!='\0')
{
len++;
last++;
}
last--;
char temp;
int nn=n%len;
for(int i=1;i<=nn;i++)
{
temp=p[len-1];
for(int i=len-1;i>=1;--i)
{
p[i]=p[i-1];
}
p[0]=temp;
}
}
//给定字符串"we;tonight;you;",编码实现输出"ew;thginot;uoy;"
//给定字符串"we;tonight;you",编码实现输出"ew;thginot;uoy"
void inverse2(char *p)
{
if (p==NULL||*p=='\0')
{
return;
}
char *first=p;
char *last=p;

while(*last!=';'&&*last!='\0')
{
last++;
}
last--;
char *next=last+2;
while(first<last)
{
char temp=*first;
*first=*last;
*last=temp;
first++;
last--;
}
inverse2(next);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: