【问题标题】:Multiplying a char with the amount of next integer将 char 乘以下一个整数的数量
【发布时间】:2018-12-02 05:16:34
【问题描述】:

在我开始之前,这是我的第一个问题,英语不是我的第一语言,我是 C 编程的新手。我搜索了我的问题但找不到。我的问题是如何打印字符串中的下一个整数。

例如:输入:a2b3d4e5fgh 输出:aabbbddddeeeeefgh

谢谢。

编辑:真正的任务是有一个最多包含 30 个字符的字符串,当只有带有字符的字符串时,它只会打印输入,但是当有一个字符部分以 ' 分隔时,它必须涉及字符和整数以及字符必须打印下一个整数的数量。

示例输入 1:

abcdefg

样本输出 1:

abcdefg

示例输入 2:

'a2b3c2d2'efg

示例输出 2:

aabbbccddefg

我来了

int main () {

    char number[30];
    scanf("%s", &number);
    char *part1 = strtok(number, "'");
    char *part2 = strtok(NULL, "'");
    char *part3 = strtok(NULL, "'");


    /*if (number[30]!=0){
        printf("Falsche Eingabe");
    } */





    return 0 ;
}

【问题讨论】:

  • 你试过什么?您具体需要哪些帮助?
  • 我的好奇心快要死了:a11b4c5de 会发生什么?最终输出字符串中是否有一个 a 或 11 个?无论如何,它都是从一个算法开始,然后是一些代码。我们有点前者,而后者则没有,所以改进和尝试都在你的议程上。这样做之后,如果事情不正确,你已经尝试调试你的代码,但仍然卡住,把它带到这里,我们可能可以提供帮助。

标签: c


【解决方案1】:

当然,这个问题需要一定的注意力。我不擅长 C,但我可以提出一些可用于实现此目的的逻辑。我还将提供一个 java 代码示例,我确信它可以很容易地翻译成 C。

  1. 已通过用户输入或使用默认值初始化您的字符串:

String data = "m3bdfg21tehy4h"; String newData="";

  1. 声明一个变量来存储最后一个非数字字符的位置,假设第一个字符必须是字母:

int lastNonDigitPosition=0;

  1. 声明一个布尔变量,让您知道最后验证的字符是数字还是非数字:

Boolean isLastCharDigit=false;

  1. 如果我们一次可以有超过 1 个数字,则声明一个将存储数字序列的变量:

String digitSequence="";

  1. 将 String/CharSequence 转换为字符数组 char[] chars=data.toString().toCharArray();

  2. 验证给定位置的每个字符

for (int i=0;i<chars.length;i++)
                    {
                      //verify is the current character is not digit
                        if (!Character.isDigit(chars[i]))
                        {

                            if (isLastCharDigit)
                            {
                            /*If the last char was a digit, cast the value of digitSequence to integer so you can know how many times you will repeat the character that comes before digit sequence*/
                                int digitTimes=Integer.valueOf(digitSequence);

                                //reinitialize the digit sequence 

                                digitSequence="";
                                for (int j=0;j<digitTimes;j++)
                                {
                                //concatenate the last non-digit character to newData digitTimes times 
                                    newData+=chars[lastNonDigitPosition];
                                }
                            }else{
                            //if the last char was not a digit just a the character to the String
                                newData+=chars[i];
                            }

                            //save the last non-digit position
                            lastNonDigitPosition=i;
                            //inform this current character is not digit
                            isLastCharDigit=false;

                        }else{
                           //if the character is a digit, concatenate with //digitSequence and inform the last char is digit
                            digitSequence+=chars[i];
                            isLastCharDigit=true;
                        }
                    }

这就是我可以解决问题的方法。这是JAVA示例,如果有人可以翻译成C可以很好,我试图解释逻辑以使代码更易于理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2021-12-15
    • 2014-12-14
    相关资源
    最近更新 更多