String rotation

String rotate by k position. For example, “abcdefg” and k=2. The result should be “cdefgab”. 

C++:
01 void reverse(char* start, char* end)
02 {
03     while(start<end){
04         char tmp=*start;
05         *start=*end;
06         *end=tmp;
07         start++;
08         end;
09     }
10 }
11 
12 void stringRotate(char* p, int n)
13 {
14     if(p==NULL)
15         return;
16     int len=strlen(p);
17     reverse(p,p+n1);
18     reverse(p+n,p+len1);
19     reverse(p,p+len1);
20 }
Advertisements
This entry was posted in String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s