#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <time.h>

/** 判断字符串是否以\0结束 / int isStringEndsWithEmptyChar(const char allStr) { if(allStr[strlen(allStr)-1] == '\0') { return 1; } const char thePtr = allStr; if(thePtr[strlen(allStr)]==''\0') { return 1; } return 0; } /** 转为c语言char类型 1,最后一个字符是否为\0空字符,如果是则直接转为c语言的char 2,不是根据情况是否产生新的,比如判断是否是常量区域字符串 / char toCharPtrByUtf(const char allStr,int isCanWrite) { const char* thePtr = allStr; int isHasEmpty = isStringEndsWithEmptyChar(allStr); if(!isCanWrite) { if(isHasEmpty) { return (char*)thePtr; } else { return toUTFz!(char*)(allStr); } } //不是指向常量数据区域的指针 if ( (unsigned long) thePtr >= 0x400000 && (unsigned long) thePtr <= 0x600000) { return toUTFz!(char*)(allStr); }

if(isHasEmpty)
{
    return (char*)thePtr;
}
else
{
    return toUTFz!(char*)(allStr);
}

} /** 转为c语言char类型 1,最后一个字符是否为\0空字符,如果是则直接转为c语言的char 2,总会复制一个信的字符串,利用toStringz / char toCharPtr(const char allStr,int isCanWrite) { const char* thePtr = allStr; int isHasEmpty = isStringEndsWithEmptyChar(allStr); if(!isCanWrite) { if(isHasEmpty) { return (char*)thePtr; } else { return (char*)(toStringz(allStr)); } } //不是指向常量数据区域的指针 if ( (unsigned long) thePtr >= 0x400000 && (unsigned long) thePtr <= 0x600000) { return (char*)(toStringz(allStr)); }

if(isHasEmpty)
{
    return (char*)thePtr;
}
else
{
    return (char*)(toStringz(allStr));
}

} /** 转为c语言const char类型 / const char * toConstCharPtrByUtf(const char allStr) { const char thePtr = allStr; int isHasEmpty = isStringEndsWithEmptyChar(allStr); if(isHasEmpty) { return thePtr; } else { return toUTFz!(const char )(allStr); } } /* 转为c语言const char类型 / const char * toConstCharPtr(const char allStr) { const char thePtr = allStr; int isHasEmpty = isStringEndsWithEmptyChar(allStr); if(isHasEmpty) { return thePtr; } else { return (const char *)(toStringz(allStr)); } }

// 判断两个字符是否相等,根据 isCaseSensitive 参数决定是否区分大小写 int charEqual(char a, char b, int isCaseSensitive) { if (isCaseSensitive) { return a == b; } else { return tolower(a) == tolower(b); } }

// 在字符串 haystack 中查找第一次出现 needle 的位置,返回指向该位置的指针,如果找不到则返回 null char* strstrCase(char* haystack, const char* needle, int isCaseSensitive) { if (!haystack || !needle || !*needle) { return haystack; }

size_t haystackLen = strlen(haystack);
size_t needleLen = strlen(needle);

if (needleLen > haystackLen)
{
    return NULL;
}

if (isCaseSensitive)
{
    return strstr(haystack, needle);
}

for (size_t i = 0; i <= haystackLen - needleLen; i++)
{
    int found = 1;
    for (size_t j = 0; j < needleLen; j++)
    {
        if (!charEqual(haystack[i + j], needle[j], isCaseSensitive))
        {
            found = 0;
            break;
        }
    }
    if (found)
    {
        return haystack + i;
    }
}

return NULL;

}

char* replaceCstr(const char* allStr, const char* searchStr, const char* replaceStr, int isCaseSensitive) { if (!allStr || !searchStr || !replaceStr || !searchStr) { return (char)allStr; }

char* result = NULL;

unsigned long searchStrLength = strlen(searchStr);
unsigned long replaceStrLength = strlen(replaceStr);
unsigned long count = 0;
char* p = (char*)allStr;

while ((p = strstrCase(p, searchStr, isCaseSensitive)) != NULL)
{
    count++;
    p += searchStrLength;
}

if (count == 0)
{
    return (char*)allStr;
}

unsigned long newStrLength = strlen(allStr) + count * (replaceStrLength - searchStrLength);
result = (char*) malloc(newStrLength + 1);

char* q = result;
p = (char*)allStr;

while (count-- > 0)
{
    char* r = strstrCase(p, searchStr, isCaseSensitive);
    long length = r - p;

    memmove(q, p, length);
    q += length;

    memcpy(q, replaceStr, replaceStrLength);
    q += replaceStrLength;

    p = r + searchStrLength;
}

strcpy(q, p);

return result;

}

int main() { clock_t start = clock();

const char* str = "hello, world!";
const char* oldptr = toConstCharPtr(str);
char* now = NULL;
for (int ii = 0; ii < 10000000; ii++)
{
    now = replaceCstr(oldptr,"o","O",1);
    now = replaceCstr(oldptr,"l","1",0);
    now = replaceCstr(oldptr,",","",1);
    now = replaceCstr(oldptr,"!","",1);
}
if(oldptr!=now)
{
    free(now);
}

clock_t end = clock();
printf("C语言程序运行时间  :%f秒\n", (double)(end - start) / CLOCKS_PER_SEC);

return 0;

}

D语言代码转C语言:字符串操作示例

原文地址: https://www.cveoy.top/t/topic/ohVn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录