atoi()

    xiaoxiao2022-07-13  143

    感谢:透支的双手的BLOG

    写法(同时读入数字和字符串):

    #include <iostream> #include <algorithm> using namespace std; int main(){ int n; char str[30]; scanf("%d%s",&n,str); cout<<n<<endl; int a = atoi(str); cout<<a<<endl; return 0; }​ 中间输入多少个空白和换行都可以。

    作用:将字符串中的数字提取成整型。

    特点:

    从字符串的第一位( str[0] )开始,读到第一个不是数字的字符结束,提取这部分作为整型返回。若第一位不是数字字符,返回零。

    补充:中间输入多少个空白和换行都不影响。

    #include <iostream> #include <algorithm> using namespace std; int main(){ char str[30]; while(true){ scanf("%s",str); printf("%s\n",str); } return 0; }
    最新回复(0)