2015傅富泰写——STRING权

前两天我在丰富的祝福大福参加了宣讲会。并参加了笔试,最后,一个大问题,我在这里谈解决方案,关于这些问题的含义是这样的,您只能使用c图书馆,实现一个功能void MakeString(char *pStr,int n)(ps:在这里,我现在函数名取,什么要记住的是,考试),功能声称是基于''结尾的字符串pStr。一个须要右移的字符个数n,实现类似输入这样MakeString("abcdefghi",2),字符串右移后变成:hiabcdeg;

下面给出我当时的解法,希望各位看官有什么意见或者建议多多给我指点指点。

void MakeString(char *pStr,int n)
{
char buf_heart[20] = "";
char buf_back[20] = "";
int iCount = 0;//总字符个数
int leave = 0;//头部不动向后移动的个数
int i = 0;
char *p = pStr;
while(*p != '')
{
iCount++;
p++;
}
if (n > iCount)
{
return;//超过字符串大小返回
}
leave = iCount - n;
for (i = 0;i < leave;i++)//存下原字符串被放到末尾的字符串
{
buf_back[i] = pStr[i];
}
for (i = 0;i < iCount;i++)//存下原字符串被放到头部的字符串
{
buf_heart[i] = pStr[leave];
leave++;
}
strcpy(pStr,buf_heart);
strcat(pStr,buf_back);
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/bhlsheji/p/4811221.html