c++学习笔记之字符串移位

字符串向一个方向旋转特定的位:

#include "stdafx.h"
#include "iostream"
using namespace std;

char *invert(char *start, char * end)
{
	char temp, *ptmp = start;
	while( start != NULL && end != NULL && start < end)
	{
		temp = *start;
		*start = *end;
		*end = temp;
		start++;
		end--;
	}
	return ptmp;
}

char *Left(char *arr, int num)
{
	int len = strlen(arr);
	invert(arr, arr + (num - 1));
	invert(arr + num, arr + (len - 1));
	invert(arr, arr + (len - 1));
	return arr;
}

int main()
{
	char s[] = "abcdeghij";
	int len = strlen(s);
	Left(s, 3);
	//cout << s;
	for( int i = 0; i < len; i++ )
	{
		cout << s[i];
	}
	cin.get();
	cin.get();
	return 0;
}


你可能感兴趣的:(c++学习笔记之字符串移位)