字符串循环移动的有关问题
字符串循环移动的问题
一个函数作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh” 。函数头是这样的:
void LoopMove ( char * pStr, int steps );
或者把返回值由void改成char*也行。
------解决方案--------------------
你看看对不对
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
char* LoopMove ( char * pStr, int steps )
{
char* str = pStr;
int i,j;
int length = strlen(pStr);
char a,b;
for(i = 0;i < steps;i++){
j = 0;
a = pStr[j];
while(j < length){
b = pStr[(j+1)%length];
pStr[(j+1)%length] = a;
a = b;
j++;
}
}
return str;
}
int main()
{
char buf[] = "abcef ";
char* str;
str = LoopMove(buf,1);
cout < <buf < <endl;
return 0;
}
------解决方案--------------------
等待接分,楼主给分呀,在线等待中
#include <stdio.h>
#include <string.h>
void LoopMov(char *pStr,int steps);
int main()
{
char str[] = "abcdefd ";
char *p = str;
LoopMov(p,14);
printf( "%s\n ",p);
return 0;
}
void LoopMov(char *pStr,int steps)
{
char *q;
char temp;
int len;
steps = steps % strlen(pStr) ;
len = strlen(pStr) ;
//p = pStr;
//q = pStr+len-1;
while( steps > 0 )
{
q = pStr+len-1;
temp = *q;
while (q != pStr)
{
*q = *(q-1);
q--;
}
*q = temp;
steps--;
}
}
------解决方案--------------------
#include "stdio.h "
#include "iostream.h "
void LoopMove ( char * pStr, int steps );
void main()
{
char pStr[20];
int steps;
cin> > pStr;
steps=2;
LoopMove(pStr,steps);
cout < <pStr;
}
void LoopMove ( char * pStr, int steps )
{
int max=0;
while( '\0 '!=pStr[max])
max++;
int i;
i=-1;
do
{
i++;
pStr[(max-i)+steps]=pStr[max-i];
}
while(i <max);
for(;i <(max+steps);i++)
{
pStr[i%max]=pStr[i];
}
pStr[max]= '\0 ';
}
看看这个怎么样?我觉得还凑和,想骂的就骂吧。
正好帮助我提高。
一个函数作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh” 。函数头是这样的:
void LoopMove ( char * pStr, int steps );
或者把返回值由void改成char*也行。
------解决方案--------------------
你看看对不对
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
char* LoopMove ( char * pStr, int steps )
{
char* str = pStr;
int i,j;
int length = strlen(pStr);
char a,b;
for(i = 0;i < steps;i++){
j = 0;
a = pStr[j];
while(j < length){
b = pStr[(j+1)%length];
pStr[(j+1)%length] = a;
a = b;
j++;
}
}
return str;
}
int main()
{
char buf[] = "abcef ";
char* str;
str = LoopMove(buf,1);
cout < <buf < <endl;
return 0;
}
------解决方案--------------------
等待接分,楼主给分呀,在线等待中
#include <stdio.h>
#include <string.h>
void LoopMov(char *pStr,int steps);
int main()
{
char str[] = "abcdefd ";
char *p = str;
LoopMov(p,14);
printf( "%s\n ",p);
return 0;
}
void LoopMov(char *pStr,int steps)
{
char *q;
char temp;
int len;
steps = steps % strlen(pStr) ;
len = strlen(pStr) ;
//p = pStr;
//q = pStr+len-1;
while( steps > 0 )
{
q = pStr+len-1;
temp = *q;
while (q != pStr)
{
*q = *(q-1);
q--;
}
*q = temp;
steps--;
}
}
------解决方案--------------------
#include "stdio.h "
#include "iostream.h "
void LoopMove ( char * pStr, int steps );
void main()
{
char pStr[20];
int steps;
cin> > pStr;
steps=2;
LoopMove(pStr,steps);
cout < <pStr;
}
void LoopMove ( char * pStr, int steps )
{
int max=0;
while( '\0 '!=pStr[max])
max++;
int i;
i=-1;
do
{
i++;
pStr[(max-i)+steps]=pStr[max-i];
}
while(i <max);
for(;i <(max+steps);i++)
{
pStr[i%max]=pStr[i];
}
pStr[max]= '\0 ';
}
看看这个怎么样?我觉得还凑和,想骂的就骂吧。
正好帮助我提高。