public static String insertLineFeeds(String s, int width) {

		StringBuilder dest = new StringBuilder();

		// index of start of current line
		int lineStart = 0;

		for (int i = 0, j = 0; i < s.length(); i++, j++) {

			// set c to current char
			char c = s.charAt(i);

			if (lineStart == i && c == ' ') {
				lineStart++;
			}
			// if we've exceeded the maximum line width, scan back
			// to a space

			if (j - lineStart > width) {
				int lastSpace = j - 1;
				for (; lastSpace > 0; lastSpace--) {
					if (s.charAt(lastSpace) == ' ') {
						break;
					}
				}
				int len = lastSpace - lineStart;
				boolean removeSpace = (len > 0);
				if (!removeSpace) {
					len = width;
				}

				if (dest.length() > 0) {
					dest.append("!\n");
				}
				dest.append(s.substring(lineStart, lineStart + len));

				lineStart += len + (removeSpace ? 1 : 0);
			}
		}
		return dest.toString();
	}