{
  "question_id": 7435486,
  "question_title": "将一个字符串中第一个连续数字转换成整数",
  "question_content": "<p>请编写一个fun函数&#xff0c;实现如下功能&#xff1a;将一个字符串中第一个连续数字转换成整数&#xff0c;作为函数值返回&#xff0c;否则返回0&#xff08;程序的输入输出要有提示&#xff09;\n比如&#xff1a;字符串中的内容为:&#34;abc123 def45gh&#34;&#xff0c;则函数的返回值为123。</p>",
  "difficulty": "中等",
  "answer_id": 53406098,
  "answer_content": "<p>为什么不写注释&#xff1f;</p>\n\n<p>懒得写。。。 </p>\n\n<pre>\n<code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;string&gt;\n\nusing namespace std;\n\nint fun(string str);\n\nint main(void) {\n\tstring str &#61; &#34;abc123 def45gh&#34;;\n\n\tcout &lt;&lt; fun(str);\n\n\n\treturn 0;\n}\n\n\nint fun(string str) {\n\tint index &#61; -1;\n\tint score &#61; 0;\n\n\tfor (int i &#61; 0; i &lt; str.length(); i&#43;&#43;) {\n\t\tif ((str[i] &gt;&#61; 48 &amp;&amp; str[i] &lt;&#61; 57) &amp;&amp; (i &#43; 1 &lt; str.length()) &amp;&amp; (str[i &#43; 1] &gt;&#61; 48 &amp;&amp; str[i &#43; 1] &lt;&#61; 57)) {\n\t\t\tindex &#61; i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (index &#61;&#61; -1) {\n\t\treturn score;\n\t}\n\n\tscore &#61; str[index] - &#39;0&#39;;\n\tfor (int i &#61; index &#43; 1; i &lt; str.length(); i&#43;&#43;) {\n\t\tif (str[i] &gt;&#61; 48 &amp;&amp; str[i] &lt;&#61; 57) {\n\t\t\tscore *&#61; 10;\n\t\t\tscore &#43;&#61; str[i] - &#39;0&#39;;\n\t\t\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn score;\n}</code></pre>\n\n<p> </p>\n",
  "tag_name": "c语言",
  "cpp": "#include <iostream>\n#include <string>\nusing namespace std;\nint fun(string str);\nint main(void) {\n\tstring str = \"abc123 def45gh\";\n\tcout << fun(str);\n\treturn 0;\n}\nint fun(string str) {\n\tint index = -1;\n\tint score = 0;\n\tfor (int i = 0; i < str.length(); i++) {\n\t\tif ((str[i] >= 48 && str[i] <= 57) && (i + 1 < str.length()) && (str[i + 1] >= 48 && str[i + 1] <= 57)) {\n\t\t\tindex = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (index == -1) {\n\t\treturn score;\n\t}\n\tscore = str[index] - '0';\n\tfor (int i = index + 1; i < str.length(); i++) {\n\t\tif (str[i] >= 48 && str[i] <= 57) {\n\t\t\tscore *= 10;\n\t\t\tscore += str[i] - '0';\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn score;\n}",
  "topic_link": "https://bbs.csdn.net/topics/600469963",
  "status": 1,
  "keywords": "算法初阶,基础知识,函数的增长,标准记号与常用函数",
  "license": "csdn.net",
  "notebook": {
    "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/cpp/116.ipynb?type=file"
  },
  "notebook_enable": 1,
  "author": "Kall00"
}