【问题标题】:How to read multiple digit number from a string如何从字符串中读取多个数字
【发布时间】:2017-08-10 04:44:15
【问题描述】:

我正在尝试传递一个字符串 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 作为它的元素,而 10 是它的元素。

如何更正此代码?

【问题讨论】:

  • 1) k 应该是0 作为初始值。
  • 感谢@BLUEPIXY

标签: c string integer string-formatting


【解决方案1】:

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

#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;
}

注意事项:

  • 包括&lt;ctype.h&gt; 以使用isalpha()
  • 通过传递最大字符数来保护scanf 的目标数组并检查返回值。
  • 转换非空行的格式就是%[^\n],后面的s不正确。请注意,与fgets() 不同,如果该行为空,则此scanf() 格式将失败。
  • 您应该始终测试scanf() 的返回值。
  • char 参数转换为isalpha()isdigit()(unsigned char),以避免在char 有符号且具有负值时出现未定义的行为。
  • 使用putchar(c) 代替printf("%c", c); 输出单个字符

【讨论】:

  • 很好。我猜内部 if 循环有错字。感谢@chqrlie 的帮助
【解决方案2】:

else-block的部分必须循环。

喜欢这个

#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;
}

【讨论】:

  • 这将解析a5d4并产生aaaaadddd并在a5ab等上调用未定义的行为。
  • @chqrlie 是的,验证不足。
  • 另一方面,规格也不够;-)
  • 这很快@BLUEPIXY。我试图找出不同的方法来获得一个多位数,但都是徒劳的
  • 对不起,我应该已经指定清楚了。输入只能是交替的数字和字符。
【解决方案3】:

代码的问题是,当你在字符串中遇到一个数字时,你把它当作一个数字,并将它存储在 num 数组中。

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

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

我把代码留给你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2015-12-09
    • 2021-01-09
    相关资源
    最近更新 更多