Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, if we split text into words and paragraphs, then we can simply add word by word to output:</p> <pre><code>const int linewidth = 50; static void Main(string[] args) { using(StreamReader r = new StreamReader("text1.txt")) { using(StreamWriter w = new StreamWriter("text2.txt")) { int written = 0; while(true) { string word = ReadWord(r); if(word == null) break; //end of file if(word == "") { //end of paragraph w.Write("\r\n\r\n"); written = 0; } if(written + word.Length &gt; linewidth) { //endline w.Write("\r\n"); written = 0; int i = 0; while(word[i] == ' ') i++; w.Write(word.Substring(i)); written = word.Length - i; } else { w.Write(word); written += word.Length; } } } } } </code></pre> <p>So we need some smart "word reader":</p> <pre><code>static int c = -1; static string ReadWord(StreamReader r) { string word = ""; bool started = false; if(c == -1) c = ReadChar(r); while(true) { if(c == -1) { //eof if(word == "") return null; return word; } word += (char)c; c = r.Read(); if(c != ' ') started = true; else if(started) break; } return word; } </code></pre> <p>And this word reader needs a smart character reader, which treats all line ends as spaces and recognizes empty lines as paragraphs:</p> <pre><code>static bool lineend = false; static int ReadChar(StreamReader r) { int c = r.Read(); if(c == '\n') c = r.Read(); if(c == '\r') { if(lineend) return '\r'; lineend = true; return ' '; } lineend = false; return c; } </code></pre> <p>As you can see, I use no internal array buffers, so the program can be used for any large files, but isn't possibly as fast as the algorithm in memory with strings.</p> <p>Words longer than line are written to their own lines (see Main).</p> <p>Only spaces and CRLF are treated as word delimiters. In real word situation wyou should probably extend this to TAB or other whitespaces.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload