How to get the first N characters of a string in C++

1 Answer

0 votes
#include <iostream>
#include <string>

int main() {
    std::string s = "c++ programming";
    int N = 6;

    std::string first_n_char = s.length() > N ? s.substr(0, N) : "";

    std::cout << first_n_char;
}



/*
run:

c++ pr

*/

 



answered Sep 7, 2024 by avibootz

Related questions

1 answer 118 views
1 answer 116 views
1 answer 129 views
1 answer 180 views
1 answer 178 views
1 answer 218 views
...