算法: 给定一个字符串,在字符串中找到最长的数字串

给定一个字符串,在字符串中找到最长的数字串, 即数字组成的字符串

void OutputMaxString(char *str){
	int maxLength = 0,curLength = 0;
	int i = 0,j = 0,k = 0;
	while(str[i] != '\0'){
		while(str[i] >= '0' && str[i] <= '9'){
			curLength++;
			i++;
		}
		if(curLength > maxLength){
			maxLength = curLength;
			k = i - curLength;
			curLength = 0;
			continue;
		}
		curLength = 0;
		i++;
	}
	for(i = 0;i < maxLength;i++,k++){
		str[i] = str[k];
	}
	str[i] = '\0';
}

 

转载于:https://my.oschina.net/cotsnail/blog/801761

你可能感兴趣的:(算法: 给定一个字符串,在字符串中找到最长的数字串)