字符串處理基礎(一)反轉,循環移動

#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);
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章