I have to read in an integer which will be the length of the succeeding lines. (The lines of text will never be longer than the length provided).
I then have to read
The way I would go about this is to use a loop with regular-expression replacements.
After you do the initial replacement, I'd suggest you use a while loop, bounded on the length of the string being less than the target size. Initialize int numUnderscores = 1; outside of the while. Then the steps inside the loop will be:
"/[^_](_{" + numUnderscores + "})[^_]/" which says "any char that is not an underscore, followed by numUnderscores instances of the underscore char, followed by any char that is not an underscore".ReplaceFirst() to perform the replacementnumUnderscoresObviously, since this is a homework problem, I'm leaving the actual process of writing the code as an exercise. If you have specific questions about some piece of it, or about some component of the logic structure I described, just ask in comments!
The benefit of doing things this way is that it will work for any size string, and is very configurable for different situations.