How to initialize a vector with characters from a C-string in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <cstring> // for strlen

int main() {
    const char* str = "c++ programming";
    std::vector<char> chars(str, str + std::strlen(str));

    for (char ch : chars) {
        std::cout << ch << ' ';
    }
}



 
/*
run:
 
c + +   p r o g r a m m i n g 
 
*/

 



answered Nov 24, 2025 by avibootz
...