How to wrap a string into lines of width w in C++

2 Answers

0 votes
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
 
/*
    wrap_text()
    -----------
    Wraps a given string into lines of maximum width `w`.
 
    Approach:
    - Use std::istringstream to extract words efficiently.
    - Build lines by appending words until adding another would exceed width.
    - Push completed lines into a vector.
    - Join lines with newline characters.
 
    This avoids manual character-by-character scanning and uses
    standard library tools for clarity and performance.
*/
 
std::string wrap_text(const std::string& text, std::size_t w) {
    std::istringstream iss(text);   // Stream for reading words
    std::string word;
    std::string line;
    std::vector<std::string> lines;
 
    while (iss >> word) {
        // If the current line is empty, start it with the word
        if (line.empty()) {
            line = word;
        }
        // Otherwise check if adding the next word exceeds width
        else if (line.size() + 1 + word.size() <= w) {
            line += " " + word;
        }
        // If it exceeds width, push the current line and start a new one
        else {
            lines.push_back(line);
            line = word;
        }
    }
 
    // Push the last line if not empty
    if (!line.empty()) {
        lines.push_back(line);
    }
 
    // Join lines into a single string separated by '\n'
    std::string result;
    for (std::size_t i = 0; i < lines.size(); ++i) {
        result += lines[i];
        if (i + 1 < lines.size()) {
            result += "\n";
        }
    }
 
    return result;
}
 
int main() {
    std::string sample =
        "C++ provides powerful standard library tools for handling strings. "
        "This program demonstrates how to wrap text cleanly and efficiently.";
 
    std::string wrapped = wrap_text(sample, 35);
    std::cout << wrapped << "\n";
}
 
 
 
/*
run:
 
C++ provides powerful standard
library tools for handling strings.
This program demonstrates how to
wrap text cleanly and efficiently.
 
*/

 



answered 8 hours ago by avibootz
edited 3 hours ago by avibootz
0 votes
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

std::vector<std::string> wrap_text(const std::string& text, size_t width) {
    std::istringstream words(text);
    std::vector<std::string> lines;
    std::string word, line;

    while (words >> word) {
        if (line.size() + word.size() + (line.empty() ? 0 : 1) > width) {
            lines.push_back(line);
            line.clear();
        }
        if (!line.empty()) line += ' ';
        line += word;
    }

    if (!line.empty())
        lines.push_back(line);

    return lines;
}

int main() {
    std::string s = "C++ provides powerful standard library tools for handling strings. "
                    "This program demonstrates how to wrap text cleanly and efficiently.";
    auto lines = wrap_text(s, 35);

    for (auto& l : lines)
        std::cout << l << "\n";
}


  
/*
run:
  
C++ provides powerful standard
library tools for handling strings.
This program demonstrates how to
wrap text cleanly and efficiently.
  
*/

 



answered 2 hours ago by avibootz
...