如何从字符串中读取多位数字

How to read multiple digit number from a string

我正在尝试将字符串 S 作为输入传递。这里字符串 S 可以包含多个整数值,后跟一个字母表。程序必须根据先前的整数值扩展字母表。

考虑输入:4a5h 其输出:aaaahhhhh,即 4 倍 a 和 5 倍 h

也用于输入:10a2b 输出:aaaaaaaaaabb,即10次a和2次b

这是我的代码:

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

int main() {
    char s[1000], alp[1000];
    int num[1000];
    int n = 0;
    int i, j, k, m;
    k = 0;
    scanf("%[^\n]s", s);//Reads string until newline character is encountered
    for (i = 0; i < strlen(s); i++) {
        if (isalpha(s[i])) {
            alp[n] = s[i]; // alp[] stores the alphabets
            n += 1;
        } else {
            num[k] = s[i] - '0';// num[] stores the numbers
            k += 1;
        }
    }
    for (i = 0; i < k; i++) {
        for (m = 0; m < num[i]; m++)
            printf("%c", alp[i]);
    }
    return 0;
}

但是使用此代码我无法读取 2 位或 3 位或 N 位数字。因此,如果输入为 100q1z,则 alp[] 数组可以,但 num[] 数组不包含 1001 作为其元素,而是 1 0 是它的元素。

如何更正此代码?

代码的问题在于,当您在字符串中遇到数字时,您将其视为数字并将其存储在 num 数组中。

如果数组中只有 个位数,这很好。

对于多位数字执行此操作-
读取数字字符串直到找到字母表,使用获得的数字组成一个数字,然后将其保存到 num array.

我把代码留给你了。

您应该修改循环以处理字符串中连续出现的数字:

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

int main(void) {
    char s[1000], alp[1000];
    int num[1000];
    int i, k = 0, m, n;

    //Read string until newline character is encountered
    if (scanf("%999[^\n]", s) == 1) {
        for (i = 0; s[i]; i++) {
            n = 1;
            if (isdigit((unsigned char)s[i])) {
                for (n = s[i++] - '0'; isdigit((unsigned char)s[i]); i++) {
                     n = n * 10 + s[i] - '0';
                }
            }
            if (isalpha((unsigned char)s[i])) {
                alp[k] = s[i];  // store the letter
                num[k] = n;   // store the number
                k += 1;
            }
        }
        for (i = 0; i < k; i++) {
            for (m = 0; m < num[i]; m++)
                putchar(alp[i]);
        }
    }
    putchar('\n');
    return 0;
}

备注:

  • 包含 <ctype.h> 以使用 isalpha().
  • 通过传递最大字符数并检查 return 值来保护 scanf 的目标数组。
  • 非空行转换格式为%[^\n],尾部s不正确。请注意,与 fgets() 不同,如果该行为空,此 scanf() 格式将失败。
  • 您应该始终测试 scanf() 的 return 值。
  • char 参数转换为 isalpha() 并将 isdigit() 转换为 (unsigned char) 以避免在 char 已签名且具有负值时发生未定义的行为。
  • 使用putchar(c)输出单个字符而不是printf("%c", c);

else-bolock 部分必须循环。

像这样

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> //need this for isalpha and isdigit

int main(void){
    char s[1000], alp[1000];
    int num[1000];
    int m = 0, n = 0;
    int i, j;
    unsigned char ch;//convert char to unsigned char before use isalpha and isdigit

    scanf("%999[^\n]", s);//remove s after [^\n] and Add limit
    for(i = 0; ch = s[i]; i++){//replace strlen each loop
        if(isalpha(ch)){
            alp[n++] = s[i];
        } else if(isdigit(ch)){
            num[m] = 0;
            while(isdigit(ch = s[i])){
                num[m] = num[m] * 10 + s[i] - '0';
                ++i;
            }
            ++m;
            --i;//for ++i of for-loop
        } else {//Insufficient as validation
            printf("include invalid character (%c).\n", ch);
            return -1;
        }
    }
    for(i = 0; i < m; i++){
        for(j = 0; j < num[i]; j++)
            printf("%c", alp[i]);
    }
    puts("");

    return 0;
}