void strdelchr(char * ar, char ch) /* v2.8 */
{
int i = 0, j = 0;
do
{
while(ar[j] == ch)
j++;
ar[i] = ar[j++];
}while(ar[i++]);
}
/* 请大家提意见,如果有更好的方法请分享下! [我在这感谢大家!] */
[此贴子已经被作者于2007-11-20 21:23:14编辑过]
/* 做了一个测试程序 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 10000000
void strdelchr1(char * ar, char ch) /* v2.8 */
{
int i = 0, j = 0;
do
{
while(ar[j] == ch)
j++;
ar[i] = ar[j++];
}while(ar[i++]);
}
void strdelchr2(char * ar, char ch) /* 3 楼 */
{
int i=0,j=0;
while(ar[i]!='\0')
{
if(ar[i] == ch)
j++;
else
i++;
ar[i] = ar[i+j];
}
}
int main(void)
{
int i = 0;
clock_t start, end;
char * ar1 = (char *)malloc(sizeof(char) * MAX);
char * ar2 = (char *)malloc(sizeof(char) * MAX);
srand((unsigned int)time(NULL));
ar1[MAX - 1] = ar2[MAX - 1] = '\0';
while(1)
{
for(i = 0; i < (MAX - 1); i++)
ar1[i] = ar2[i] = ('0' + (rand() % 10));
puts("Start.");
start = clock();
strdelchr1(ar1, '1');
end = clock();
printf("%lf\n", (double)(end - start) / CLOCKS_PER_SEC);
start = clock();
strdelchr2(ar2, '1');
end = clock();
printf("%lf\n", (double)(end - start) / CLOCKS_PER_SEC);
puts("Enter next.");
getchar();
}
return 0;
}
[此贴子已经被作者于2007-11-20 23:15:27编辑过]