2012-04-05 2 views
1

이의 우리가 선언 된 문자열을한다고 가정 해 봅시다 있다면 ... 나는 그것이 60 개 이상의 폭을 경우 텍스트 줄 바꿈하려면이 문자열 변수로랩 For 루프와 문자열이나 문

string paragraphy = "This is a really really long string containing a paragraph long content. I want to wrap this text by using a for loop to do so."; 

하고있는 경우 60 너비 이후의 공간.

코드 작성이나 도움을받을 수있는 사람이 있습니까?

+0

"둘러보기"란 적절한 위치에'\ n'을 삽입하는 것을 의미합니까? – Mysticial

+0

예. 그래서 폭 60과 공간 뒤에 감싸 야합니다. – user1087935

+0

일반적인 숙제 문제와 비슷하게 들릴 수 있습니다. – AusCBloke

답변

0

이 문제를 해결하는 기본적인 아이디어는 해당 세그먼트의 60 번째 문자 앞의 문자열 세그먼트에서 마지막 공간을 추적하는 것입니다. 이 숙제이기 때문에

, 난 당신이 코드를 마련 할 것이다, 그러나 여기에 위의 제안의 일부 거친 의사 코드입니다 :

- current_position = start of the string 
- WHILE current_position NOT past the end of the string 
    - LOOP 1...60 from the current_position (also don't go past the end of the string) 
     - IF the character at current_position is a space, set the space_position to this position 
    - Replace the character (the space) at the space_position with a newline 
    - Set the current_position to the next character after the space_position 

- If you're printing the string rather than inserting newline characters into it, you would print any remaining part of the string here. 

또한 사건을 고려하는 것이 좋습니다는 어디 돈 한 블록에 60 자의 공백이 없습니다.

+0

감사합니다! 나는 내 자신의 & 게시 코드를 게시하려고합니다. – user1087935