# 将一个字符串中第一个连续数字转换成整数

<p>请编写一个fun函数&#xff0c;实现如下功能&#xff1a;将一个字符串中第一个连续数字转换成整数&#xff0c;作为函数值返回&#xff0c;否则返回0&#xff08;程序的输入输出要有提示&#xff09;
比如&#xff1a;字符串中的内容为:&#34;abc123 def45gh&#34;&#xff0c;则函数的返回值为123。</p>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <iostream>
#include <string>
using namespace std;
int fun(string str);
int main(void)
{
    string str = "abc123 def45gh";
    cout << fun(str);
    return 0;
}
int fun(string str)
{
    int index = -1;
    int score = 0;
    for (int i = 0; i < str.length(); i++)
    {
        _____________________;
    }
    if (index == -1)
    {
        return score;
    }
    score = str[index] - '0';
    for (int i = index + 1; i < str.length(); i++)
    {
        if (str[i] >= 48 && str[i] <= 57)
        {
            score *= 10;
            score += str[i] - '0';
        }
        else
        {
            break;
        }
    }
    return score;
}
```

## template

```cpp
#include <iostream>
#include <string>
using namespace std;
int fun(string str);
int main(void)
{
    string str = "abc123 def45gh";
    cout << fun(str);
    return 0;
}
int fun(string str)
{
    int index = -1;
    int score = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57))
        {
            index = i;
            break;
        }
    }
    if (index == -1)
    {
        return score;
    }
    score = str[index] - '0';
    for (int i = index + 1; i < str.length(); i++)
    {
        if (str[i] >= 48 && str[i] <= 57)
        {
            score *= 10;
            score += str[i] - '0';
        }
        else
        {
            break;
        }
    }
    return score;
}
```

## 答案

```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57))
{
	index = i;
	break;
}
```

## 选项

### A

```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57))
{
	index = i;
	continue;
}
```

### B

```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i] >= 48 && str[i] <= 57))
{
	index = i;
	break;
}
```

### C

```cpp
if ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i] >= 48 && str[i] <= 57))
{
	index = i;
	continue;
}
```