题解 | #截取字符串#

截取字符串

https://www.nowcoder.com/practice/a30bbc1a0aca4c27b86dd88868de4a4a

///////////////////普通for循环通过索引读取/////////////////
// #include <iostream>
// #include<string>
// using namespace std;

// int main() {
//     string a;
//     string strOut;
//     int k;
//     //a和k以空白符换行符作为停止读入的分隔线
//     while (cin >> a >> k) {
//         for (int i = 0; i < k; i++) {
//             strOut += a[i];//索引访问字符串数组
//         }
//         cout << strOut << endl;
//         strOut.clear();//清空字符串
//     }
    
//     return 0;
// }

//////////////////通过string的substr函数进行字符串截取////////////////////
#include <iostream>
#include<string>
using namespace std;

int main() {
    string s;
    int k;
    while (cin >> s >> k)
    {
        //substr( int pos位置,int 个数n)
        cout << s.substr(0, k) << endl;
    }
    return 0;
}

全部评论

相关推荐

一tiao酸菜鱼:秋招还没正式开始呢,就准备有结果了。。。。?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务