Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 276525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:56:03+00:00 2026-05-12T00:56:03+00:00

Hey guys, I’m writing a word wrap function to format console text in C++.

  • 0

Hey guys, I’m writing a word wrap function to format console text in C++. My problem is either A) I don’t understand exactly what std::string::iterators do, or B) one of my iterators is not being set properly. Can anyone shed some light on the reason this code fails?

by the way: sorry if this goes into too much detail. I’m not sure if most programmers (I’m a “newbie”) have a C++ compiler installed on their machine.

std::string wordWrap(std::string sentence, int width)
{    
   //this iterator is used to optimize code; could use array indice 
   //iterates through sentence till end 
   std::string::iterator it = sentence.begin();
   //this iterator = it when you reach a space; will place a newline here
   //if you reach width;
   std::string::iterator lastSpace = sentence.begin();

   int distanceToWidth = 0;

   while (it != sentence.end())
   {
      while (it != sentence.end() && distanceToWidth < width)
      {
         if (*it == ' ')
         {
           lastSpace = it;
         }

         distanceToWidth++;
         it++;
     }

     distanceToLength = 0;
     *lastSpace = '\n';   

      //skip the space
      if (it != sentence.end())
      {
         it++;
      }
   }

   return sentence;    
}

I’m not getting correct output. Assuming I called it like this:

std::cout << wordWrap("a b c abcde abcdef longword shtwd", 5) << std::endl << std::endl;
std::cout << wordWrap("this is a sentence of massive proportions", 4) << std::endl;

I get unsatisfying output of this:

a b 
c
abcde
abcdef
longword
shtwd

//yes I get his, instead of this
his is
a
sentence
of
massive
proportions
Press any key to continue . . .

My problem is that I’m am getting newlines when inappropiate. I am getting newlines too often, and I don’t see any obvious error as to why that is. I was hoping someone independent (I’ve spent a few hours on this algorithm, and to not have the right results is quite frusturating) of the problem could look at it. Also, any obvious optimization tips?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:56:03+00:00Added an answer on May 12, 2026 at 12:56 am

    The problem is that the word this is 4 characters, and you are wrapping at four characters. So it is trying to wrap before it has set lastSpace to something reasonable.

    Look at it from the point of stepping through the code:

    lastSpace points to the "t" from the beginning of "this"
    distanceToWidth=0
    iterator=this is
             ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=1
    iterator=this is
              ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=2
    iterator=this is
               ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=3;
    iterator=this is
                ^
    
    check if we should loop (distanceToWidth<4)
    is the current character a space? no
    distanceToWidth=4;
    iterator=this is
                 ^
    
    check if we should loop (distanceToWidth<4) NO! Because distanceToWidth equals four!
    
    We break out of the loop.
    
    Recall that lastSpace was never modified it still points to the first character in the string!
    now we set the "t" character from "this" to be a newline!!
    

    ETC

    Thus we output an extra newline instead of the “t” in “this”

    about fixing it… well… you can figure it out

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer without a single primary key field, I think your best… May 12, 2026 at 8:50 am
  • Editorial Team
    Editorial Team added an answer A few things here: When one project depends on another,… May 12, 2026 at 8:50 am
  • Editorial Team
    Editorial Team added an answer "Could someone log into this page and not need the… May 12, 2026 at 8:50 am

Related Questions

hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys, I want to ask if there is an equivalent to the @@DBTS
Hey guys, I'm researching something for a friend of mine who's involved in road
Hey guys, I don't know RegExp yet. I know a lil about it but
Hey guys, I have a problem. I have situation where domain in m_domainTable starts

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.