/*
* 判断字符串是不是回文字符串
*/
bool IsPalindrone(const char *s,int n)
{
if(s == NULL || n < 1)
{
return false;
}
char *first,char *second;
int m = (n/2-1) > 0?(n/2-1):0;
first = s + m;//移位
second = s+n-1-m;
while (first >= s)
{
/* code */
if(*first-- != *second++)
{
return false;
}
}
return true;
}
/*
* 实现旋转部分字符串,比如abcdefg->efgabcd
* 要旋转的两部分先分别旋转,然后整体翻转
*/
void ReversionString(char *s,int from,int to)
{
while (from < to)
{
/* code */
char temp = s[from];
s[from++] = s[to];
s[to--] = temp;
}
}
void RotationString(char *s,int len,int m)
{
m = m % len;
ReversionString(s,0,m-1);
ReversionString(s,m,len-1);
ReversionString(s,0,len-1);
}
/*
查找字符串中出现次数最多的字符和出现的次数
*/
int main()
{
const char* ps = "hahhah";
char c = '\0';
size_t counts[256] = { 0 };
for( const char* p=ps; *p; ++p )
{
++counts[*p];
if( counts[*p] > counts[c] )
c = *p;
}
printf( "Character=%c, count=%zu\n", c, counts[c] );
return 0;
}
/*
去掉句子里面的空格
*/
#include <iostream>
using namespace std;
int main()
{
char a[40] = "i have a dream ";
int i;
int num = 0;
for (i = 0; a[i] != '\0'; ++i)
{
if (a[i] == ' ')
{
++num;
}
else
{
a[i - num] = a[i];
}
}
a[i - num] = '\0';
cout << a << endl;
system("PAUSE");
return 0;
}
/*
将空格替换成其他字符
*/
#include <iostream>
using namespace std;
void ReplaceBlank(char string[],int length)
{
if (string == nullptr && length <= 0)
{
return;
}
int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while (string[i] != '\0')
{
++originalLength;
if (string[i] == ' ')
{
numberOfBlank++;
}
++i;
}
int newLength = originalLength + numberOfBlank * 2;
if (newLength > length)
{
return;
}
int indexOfOriginal = originalLength;
int indexOfNew = newLength;
while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
{
if (string[indexOfOriginal] == ' ')
{
string[indexOfNew--] = '0';
string[indexOfNew--] = '2';
string[indexOfNew--] = '%';
}
else
{
string[indexOfNew--] = string[indexOfOriginal];
}
--indexOfOriginal;
}
}
int main()
{
const int length = 100;
char string[length] = "hello world";
ReplaceBlank(string, length);
system("PAUSE");
return 0;
}
本文由 Ryan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2020/11/12 10:42