问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

C语言中截取字符串数字的多种方法详解

创作时间:
作者:
@小白创作中心

C语言中截取字符串数字的多种方法详解

引用
1
来源
1.
https://docs.pingcode.com/baike/1105662

在C语言编程中,有时我们需要从字符串中提取数字。本文将介绍几种常见的方法,包括使用C语言的字符串处理函数、正则表达式库以及手动遍历字符串等。通过具体的代码示例,帮助读者掌握这些方法的实现原理和应用场景。

一、使用C语言的字符串处理函数

C语言提供了一系列字符串处理函数,如strtokstrchrstrstr等,这些函数可以帮助我们方便地处理字符串。在截取字符串中的数字时,这些函数可以派上用场。

1. 使用strtok函数

strtok函数用于将字符串分割成子串。我们可以利用这个函数,结合字符数组和循环,来提取字符串中的数字。

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

void extract_numbers(const char *str) {
    char temp[100];
    strcpy(temp, str);
    char *token = strtok(temp, " ");
    while (token != NULL) {
        if (isdigit(token[0])) {
            printf("%s\n", token);
        }
        token = strtok(NULL, " ");
    }
}

int main() {
    const char *str = "The price is 100 dollars and 50 cents";
    extract_numbers(str);
    return 0;
}

在这个例子中,我们将字符串按空格分割,然后检查每个子串的第一个字符是否为数字,如果是,则打印出来。

2. 使用strchr函数

strchr函数用于查找字符串中第一次出现的指定字符,我们可以用它来找到数字并提取。

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

void extract_numbers(const char *str) {
    while (*str) {
        if (isdigit(*str)) {
            putchar(*str);
        }
        str++;
    }
    putchar('\n');
}

int main() {
    const char *str = "The price is 100 dollars and 50 cents";
    extract_numbers(str);
    return 0;
}

在这个例子中,我们遍历字符串并打印出所有的数字字符

二、使用正则表达式库

C标准库中并没有直接支持正则表达式的函数,但我们可以使用POSIX正则表达式库来处理字符串。

1. 使用POSIX正则表达式库

POSIX提供了regex.h库,我们可以使用它来匹配和提取字符串中的数字。

#include <stdio.h>
#include <regex.h>

void extract_numbers(const char *str) {
    regex_t regex;
    regmatch_t pmatch[1];
    const char *pattern = "[0-9]+";
    int ret;

    ret = regcomp(&regex, pattern, REG_EXTENDED);
    if (ret) {
        printf("Could not compile regex\n");
        return;
    }

    while (!regexec(&regex, str, 1, pmatch, 0)) {
        for (int i = pmatch[0].rm_so; i < pmatch[0].rm_eo; i++) {
            putchar(str[i]);
        }
        putchar('\n');
        str += pmatch[0].rm_eo;
    }

    regfree(&regex);
}

int main() {
    const char *str = "The price is 100 dollars and 50 cents";
    extract_numbers(str);
    return 0;
}

在这个例子中,我们使用正则表达式匹配字符串中的数字,并打印出来

三、手动遍历字符串

手动遍历字符串是最直接的方法,通过遍历每个字符来检查它是否是数字,然后提取出来。

1. 基本的手动遍历

#include <stdio.h>
#include <ctype.h>

void extract_numbers(const char *str) {
    while (*str) {
        if (isdigit(*str)) {
            putchar(*str);
        }
        str++;
    }
    putchar('\n');
}

int main() {
    const char *str = "The price is 100 dollars and 50 cents";
    extract_numbers(str);
    return 0;
}

在这个例子中,我们遍历字符串并打印出所有的数字字符

2. 处理多位数字

有时候,我们需要提取完整的多位数字,而不是单个字符。

#include <stdio.h>
#include <ctype.h>

void extract_numbers(const char *str) {
    while (*str) {
        if (isdigit(*str)) {
            while (isdigit(*str)) {
                putchar(*str);
                str++;
            }
            putchar('\n');
        } else {
            str++;
        }
    }
}

int main() {
    const char *str = "The price is 100 dollars and 50 cents";
    extract_numbers(str);
    return 0;
}

在这个例子中,我们不仅提取单个数字字符,还能提取多位数字

四、使用更复杂的数据结构和算法

在实际应用中,特别是在处理复杂的字符串时,我们可能需要使用更复杂的数据结构和算法来确保代码的高效性和健壮性。

1. 使用动态数组

动态数组可以帮助我们存储和处理提取出来的数字。

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

void extract_numbers(const char *str) {
    char *numbers = malloc(strlen(str) + 1);
    int index = 0;

    while (*str) {
        if (isdigit(*str)) {
            numbers[index++] = *str;
        }
        str++;
    }

    numbers[index] = '\0';
    printf("%s\n", numbers);
    free(numbers);
}

int main() {
    const char *str = "The price is 100 dollars and 50 cents";
    extract_numbers(str);
    return 0;
}

在这个例子中,我们使用动态数组来存储提取出的数字,并在最后打印出来。这种方法可以处理任意长度的数字串,具有更好的灵活性和扩展性。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号