/*
* Created on: 2011-9-8
* Author: zhujianshe
*/
#include<stdio.h>
#include<stdlib.h>
void
take_num(const char *strIn,int *n, unsigned *outArray)
{
char ch;
*n = 0;
int index = 0;
while((ch = *(strIn+index++)) !='\0')
{
if(ch >='0'&&ch<='9')
{
int temp = 0;
temp +=ch-'0';
while((ch = *(strIn+index++)) !='\0'&&ch >='0'&&ch<='9')
{
temp *=10;
temp+=ch-'0';
}
outArray[(*n)++] = temp;
}
}
}
int
delete_sub_str(const char *str, const char *sub_str, char *result_str)
{
int count =0;
int str_index = 0,sub_str_index = 0,result_index =0;
char ch;
int isequal = 1;
while((ch = *(str+str_index++))!='\0')
{
/*判断是否跟sub_str 的第一个字符相同,如果相同就依次比较,不相同就把结果烤到result_str.
* tmp_index 记录第一个字符相同的位置,在以后比较的过程中发现从此处开始的字符串与子串不相同时
* 下一次从此处开始。在子串比较的过程中设置了一个标志符,如果不相同就从tmp_index出开始继续比较,
* 如果相同就把count 加1,并把偏移量设置为tmp_index,
*/
if(ch == *sub_str)
{
sub_str_index = 1;
int tmp_index = str_index;
while((*(str+tmp_index))!='\0'&&*(sub_str+sub_str_index)!='\0')
{
if((*(str+tmp_index++)) != *(sub_str+sub_str_index++))
{
isequal =0;
break;
}
}
if(!isequal)
{
*(result_str+result_index++) = ch;
}else
{
str_index = tmp_index;
count ++;
}
}else
{
*(result_str+result_index++) = ch;
}
}
*(result_str+result_index) = '\0';
return count;
}
int
main(void)
{
char * strIn = "ab00cd+123fght456-25 3.005fgh";
int * n = malloc(sizeof(int));
unsigned outArray[10];
take_num(strIn,n,outArray);
for(int i = 0;i<*n;i++)
{
printf("%d\t",outArray[i]);
}
printf("counts: %d\n",*n);
free(n);
char * str = "abcde123abcd123";
char *sub_str = "123";
char * result_str = malloc(sizeof(char)*100 );
int count = 0;
count = delete_sub_str(str,sub_str,result_str);
printf("str=%s\n sub_str=%s\n result_str=%s\n count=%d\n",str,sub_str,result_str,count);
sub_str = "1234";
count = delete_sub_str(str,sub_str,result_str);
printf("str=%s\n sub_str=%s\n result_str=%s\n count=%d\n",str,sub_str,result_str,count);
free(result_str);
return EXIT_SUCCESS;
}
* Created on: 2011-9-8
* Author: zhujianshe
*/
#include<stdio.h>
#include<stdlib.h>
void
take_num(const char *strIn,int *n, unsigned *outArray)
{
char ch;
*n = 0;
int index = 0;
while((ch = *(strIn+index++)) !='\0')
{
if(ch >='0'&&ch<='9')
{
int temp = 0;
temp +=ch-'0';
while((ch = *(strIn+index++)) !='\0'&&ch >='0'&&ch<='9')
{
temp *=10;
temp+=ch-'0';
}
outArray[(*n)++] = temp;
}
}
}
int
delete_sub_str(const char *str, const char *sub_str, char *result_str)
{
int count =0;
int str_index = 0,sub_str_index = 0,result_index =0;
char ch;
int isequal = 1;
while((ch = *(str+str_index++))!='\0')
{
/*判断是否跟sub_str 的第一个字符相同,如果相同就依次比较,不相同就把结果烤到result_str.
* tmp_index 记录第一个字符相同的位置,在以后比较的过程中发现从此处开始的字符串与子串不相同时
* 下一次从此处开始。在子串比较的过程中设置了一个标志符,如果不相同就从tmp_index出开始继续比较,
* 如果相同就把count 加1,并把偏移量设置为tmp_index,
*/
if(ch == *sub_str)
{
sub_str_index = 1;
int tmp_index = str_index;
while((*(str+tmp_index))!='\0'&&*(sub_str+sub_str_index)!='\0')
{
if((*(str+tmp_index++)) != *(sub_str+sub_str_index++))
{
isequal =0;
break;
}
}
if(!isequal)
{
*(result_str+result_index++) = ch;
}else
{
str_index = tmp_index;
count ++;
}
}else
{
*(result_str+result_index++) = ch;
}
}
*(result_str+result_index) = '\0';
return count;
}
int
main(void)
{
char * strIn = "ab00cd+123fght456-25 3.005fgh";
int * n = malloc(sizeof(int));
unsigned outArray[10];
take_num(strIn,n,outArray);
for(int i = 0;i<*n;i++)
{
printf("%d\t",outArray[i]);
}
printf("counts: %d\n",*n);
free(n);
char * str = "abcde123abcd123";
char *sub_str = "123";
char * result_str = malloc(sizeof(char)*100 );
int count = 0;
count = delete_sub_str(str,sub_str,result_str);
printf("str=%s\n sub_str=%s\n result_str=%s\n count=%d\n",str,sub_str,result_str,count);
sub_str = "1234";
count = delete_sub_str(str,sub_str,result_str);
printf("str=%s\n sub_str=%s\n result_str=%s\n count=%d\n",str,sub_str,result_str,count);
free(result_str);
return EXIT_SUCCESS;
}