我需要分析字串并根據寬度和特定規則將其放在框架內的中心。
- 行首至少要有一個空格,行尾至少要有一個空格。
- 如果需要添加的空格數為奇數,則應在右側添加多余的空格。
- 如果整個單詞無法容納在行中,則將在下一行列印單詞。
- 如果單詞長于行寬,則應將單詞分成幾部分,然后將它們列印在一行中,以使其完整。默認是將單詞分成兩部分。如果這是不可能的,單詞會根據行的寬度按比例拆分。
- 不允許使用輔助字串。
示例 1:
const char text[]="This is word with four characters";
int width = 20;
********************
* This is word *
* with four *
* characters *
********************
示例 2:
const char text[]="This word Thyroparathyroidectomized is too long for frame";
int width = 20;
********************
* This word *
* Thyroparathy- *
* roidectomized is *
* too long for *
* frame *
********************
示例 3:
const char text[]="This word Thyroparathyroidectomized is too long for frame";
int width = 10;
**********
* This *
* word *
* Thyro- *
* parat- *
* hyroi- *
* decto- *
* mized *
* is too *
* long *
* for *
* frame *
**********
代碼:
#include <stdio.h>
#include <string.h>
void cut_words(const char *text, int width) {
char *s = (char *)text;
// skip leading spaces
while (*s && *s == ' ')
s ;
while (strlen(s) > width) {
// *s must be nonspace and not EOS
char *e = s width - 1;
// *e is last char that can possibly fit
// *e cannot be EOS nor can e[1]
// back up to nonspace if at space
while (*e == ' ')
e--;
// *e is inside or end of word
if (e[1] != ' ') {
// not end of word; must be inside
// back up to prev space
while (e > s && *e != ' ')
e--;
// if e == s then no word break
// must assume word is longer than width
if (e == s)
e = s width - 1;
else
// find end of prev word
while (e > s && *e == ' ')
e--;
}
// *e is end of word
int k = e - s 1;
printf("* %*.*s *\n", k, k, s);
s = k;
while (*s && *s == ' ')
s ;
}
if (*s)
printf("* %s *\n", s);
}
void framed(const char *text, int width) {
int i;
for (i = 0; i < width; i )
printf("*");
printf("\n");
cut_words(text, width - 4);
for (i = 0; i < width; i )
printf("*");
}
int main() {
const char text[] = "This is word with four characters";
int width = 20;
framed(text, width);
return 0;
}
我的輸出:
********************
* This is word *
* with four *
* characters *
********************
********************
* This word *
* Thyroparathyroid *
* ectomized is too *
* long for frame *
********************
**********
* This *
* word *
* Thyrop *
* arathy *
* roidec *
* tomize *
* d is *
* too *
* long *
* for *
* frame *
**********
你能幫我把單詞排成一行嗎?
uj5u.com熱心網友回復:
在您的代碼中,您有:
int k = e - s 1;
printf("* %*.*s *\n", k, k, s);
和
if (*s)
printf("* %s *\n", s);
但這根本沒有試圖填補這些詞。要添加填充,您需要在單詞前后列印一定數量的空格。您可以像列印字串的一定數量的字符一樣執行此操作 ( %*.*s)
您需要將這些代碼替換為
int k = e - s 1;
int b = (width-k)/2;
int a = (width-k)/2 (width-k)%2;
printf("* %*.*s%*.*s%*.*s *\n", b, b, "", k, k, s, a, a, "");
和
if (*s) {
k = strlen(s);
b = (width - k) / 2;
a = (width - k) / 2 (width - k) % 2;
printf("* %*.*s%*.*s%*.*s *\n", b, b, "", k, k, s, a, a, "");
}
這解決了居中問題。但是您并沒有將單詞連字符 - 您只是將它們切成碎片。所以你需要考慮連字符。
在要剪切單詞時使部分更小,以便為連字符留出空間,您可以更改
// if e == s then no word break
// must assume word is longer than width
if (e == s)
e = s width - 1;
到
// if e == s then no word break
// must assume word is longer than width
if (e == s)
e = s width - 2;
然后你可以改變
k = e - s 1;
b = (width - k) / 2;
a = (width - k) / 2 (width - k) % 2;
printf("* %*.*s%*.*s%*.*s *\n", b, b, "", k, k, s, a, a, "");
到
k = e - s 1;
b = (width - k) / 2;
a = (width - k) / 2 (width - k) % 2;
if (lenght_prev_word(s) > width)
printf("* %*.*s%*.*s-%*.*s *\n", b, b, "", k, k, s, a-1, a-1, "");
else
printf("* %*.*s%*.*s%*.*s *\n", b, b, "", k, k, s, a, a, "");
只有當單詞長于寬度時才會列印連字符。
The full program be found at https://onlinegdb.com/4_9eQwA_H
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/436455.html
上一篇:正則運算式檢測包含特殊字符的字串
下一篇:無法從另一個專案訪問變數
