请将以下golang语言代码转为c语言对应版本然我运行测试package mainimport fmt strings timefunc main start = timeNow str = hello world! for i = 0; i 10000000; i++ str = stringsReplaceAllstr o 0
#include <stdio.h> #include <string.h> #include <time.h>
char* reverse(char* str);
int main() { clock_t start = clock();
char str[] = "hello, world!";
for (int i = 0; i < 10000000; i++) {
char* tempStr = strstr(str, "o");
while (tempStr != NULL) {
*tempStr = '0';
tempStr = strstr(tempStr + 1, "o");
}
tempStr = strstr(str, "l");
while (tempStr != NULL) {
*tempStr = '1';
tempStr = strstr(tempStr + 1, "l");
}
tempStr = strstr(str, ",");
while (tempStr != NULL) {
memmove(tempStr, tempStr + 1, strlen(tempStr));
tempStr = strstr(tempStr, ",");
}
tempStr = strstr(str, "!");
while (tempStr != NULL) {
memmove(tempStr, tempStr + 1, strlen(tempStr));
tempStr = strstr(tempStr, "!");
}
reverse(str);
}
clock_t end = clock();
printf("C语言程序运行时间:%ldms\n", (end - start) * 1000 / CLOCKS_PER_SEC);
return 0;
}
char* reverse(char* str) { int length = strlen(str); for (int i = 0, j = length - 1; i < j; i++, j--) { char temp = str[i]; str[i] = str[j]; str[j] = temp; } return str;
原文地址: https://www.cveoy.top/t/topic/fqAU 著作权归作者所有。请勿转载和采集!