/* 字符串逆转 */
void my_reverse(char *p)
{
int len = strlen(p);
for (int i = 0;i < len/2; ++i) {
int tmp = p[i];
p[i] = p[len-1-i];
p[len-1-i] = tmp;
}
}
/* 小写转大写 */
void atoA(char *p)
{
while (*p) {
if (*p > 'a' && *p < 'z') {
*p = *p - 'a' + 'A';
p++;
}
else {
p++;
continue;
}
}
}
/* 大写转小写 */
void Atoa(char *p)
{
while (*p) {
if (*p > 'A' && *p < 'Z') {
*p = *p - 'A' + 'a';
p++;
}
else {
p++;
continue;
}
}
}
/* 字符串拼接 */
void my_strcat(char *p1, const char *p2)
{
char *p = p1;
while (*p) p++;
while (*p2) {
*p = *p2;
p++;
p2++;
}
*p = '\0';
}
/* 字符串拷贝 */
void my_strcpy(char *p1, const char *p2)
{
while (p1 != NULL && *p2) {
*p1++ = *p2++;
}
*p1 = '\0';
}
/* 数字转字符串 */
void my_itoa(char *p1, const int number)
{
char *p = p1;
int num = number;
while (num) {
int n = num%10;
*p1 = n + '0';
p1++;
num /= 10;
}
*p1 = '\0';
my_reverse(p);
}
/* 字符串转数字 */
int my_atoi(const char *p1)
{
static int n = 0;
while (*p1) {
n *= 10;
n += *p1 - '0';
p1++;
}
return n;
}
/* 字符串排序(小->大) */
char *strSort(char *str)
{
int len = strlen(str);
for (int i = 0; i < len-1; ++i) {
for(int j = 0; j < len-i-1; ++j){
if(str[j] > str[j+1]){
char tmp = str[j];
str[j] = str[j+1];
str[j+1] = tmp;
}
}
}
return str;
}
字符串相关算法
最新推荐文章于 2024-10-01 04:50:10 发布