0
点赞
收藏
分享

微信扫一扫

从字符串中过滤数字和字母并输出

夏侯居坤叶叔尘 2022-01-05 阅读 69
gnuc语言

需求:从字符串中过滤数字和字母

代码实现:

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

int analysis_data(const char *str, int *value, int *value_count, char *operator, int *operator_count)
{
	int value_index = 0;
	int operator_index = 0;
	int n = 0;

	if (str == NULL || value == NULL || value_count == NULL || operator == NULL || operator_count == NULL) {
		return -1;
	}

	while (*str != '\0') {
		n = 0;
		//while (*str >= '0' && *str <= '9') {
			//n = n * 10 + (*str - '0');
			//str++;
		//	operator[operator_index++] = *str;
		//	str++;
		//}
		//while (*str >= 'A' && *str <= 'Z') {
			//n = n * 10 + (*str - '0');
			//str++;
		//	operator[operator_index++] = *str;
		//	str++;
		//}
		if ((*str >= 'a' && *str <= 'z')||(*str >= '0' && *str <= '9')|| (*str >= 'A' && *str <= 'Z')) {
			//n = n * 10 + (*str - '0');
			//str++;
			operator[operator_index++] = *str;
			str++;
		}else {
            str++;
        }
		//if (*str == '+' || *str == '-' || *str == '*' || *str == '/') {
		//	operator[operator_index++] = *str;
		//	str++;
		//}

		//value[value_index++] = n;
	}

	*value_count = value_index;
	*operator_count = operator_index;

	return 0;
}

int main()
{
	char buffer[1024] = "100+200-asdcsdfdf-dfsd_50*2/5FD/99-11";

	int value[1024];
	int value_count = 0;
	char operator[1024];
	int operator_count = 0;
	//int result = 0;

	memset(value, 0x00, sizeof(value));
	memset(operator, 0x00, sizeof(operator));

	(void)analysis_data(buffer, value, &value_count, operator, &operator_count);

	int i = 0;
	for (i = 0; i < value_count; i++) {
		printf("value: %d\n", value[i]);
	}

	for (i = 0; i < operator_count; i++) {
		printf("operator: %c\n", operator[i]);
	}
	printf("operator: %s\n", operator);

	return 0;
}

运行结果:

 

举报

相关推荐

0 条评论