我想在column = 0
之前为每个空格添加一个空格。我不知道该怎么做。
问题如下。如果你看一份报纸,你会看到写作是合理的,以适应列。写一个程序 读取报纸中列的宽度,然后读取一行文本。证明文本行适合 一列宽度。程序运行时,屏幕应如下所示:
Enter the width of the column: 40
Enter a line of text: Good morning how are you?
12345678901234567890123456789012345678901234567890...
Good morning how are you?
通过计算文本中的空白数量来完成辩护。在上面的例子中,有4个空白。然后每个间隙必须添加空格。必须尽可能均匀地分享额外空格的数量。在上面的例子中,前三个间隙各有5个空格,最后一个间隙有4个空格。
注意:
由于
#include <stdio.h>
int clear_input_buffer(void);
int column;
int c;
int g;
int e;
int space;
int length;
char line[40];
int main(){
g = 0;
printf("enter width of column\n");
scanf("%d", &column);
printf("enter line of text\n");
clear_input_buffer();
gets(line);
c = 0;
while(c <= column){
if(g <= 9)
{
printf("%d", g);
g = g + 1;
c = c + 1;
}
else
{
g = 0;
printf("%d", g);
g = g + 1;
c = c + 1;
}
}
printf("\n%s", line);
space = 0;
length = 0;
for( e = 0; line[e] != '\0'; e++ )
{
length = length + 1;
if( line[e] == ' ' )
space = space + 1;
}
column = column - length;
for( e = 0; line[e] != '\0'; e++ )
{
if((line[e] == ' ') && (column > 0))
{
add space to here
column = column - 1;
}
}
printf("%d\n", space);
printf("%d", length);
printf("%s", line);
}
int clear_input_buffer(void) {
int ch;
while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
return ch;
}
答案 0 :(得分:2)
这就是我所做的。这远非理想,但你明白了。 您只需要输入条件,例如输入的字符串大于或等于40个字符时,跳过该过程。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
int i = 0; // first just initialiaze stuff
char ch[40]; // memset the arrays, get the string
memset(ch, '\0', 40);
gets(ch);
int diff = 40 - strlen(ch);
int spaces = 0;
while(i<strlen(ch))
{
if(*(ch + i++) == ' ') // count the number of words/spaces between words
spaces++;
}
char finalt[40];
memset(finalt, '\0', 40);
i = 0;
diff /= spaces; // diff is the number of spaces to be added between every word
i = 0;
int j = 0; // j is for the finalt array
int k = 0; // k counts through the while, to put in spaces
printf("%d\n", diff);
while(i<40) // just squeeze in the spaces
{
if(ch[i] == ' ') {while(k<diff){ finalt[j++] = ' '; k++;} k = 0;}
else {finalt[j] = ch[i]; j++;}
i++;
}
printf("%s\n", finalt); // print the result
return 0;
}
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIDTH 70
#define _(x) #x
#define str(x) _(x)
void ruler_print(int n){
char ruler[] = "1234567890";
while(n>9){
printf(ruler);
n -= 10;
}
ruler[n] = '\0';
printf("%s\n", ruler);
}
int count_word(const char *text, size_t *count_char){
int i;
char *wk, *p;
p=wk=strdup(text);
*count_char=0;
for(i=0;p=strtok(p, " ");++i,p=NULL){
*count_char+=strlen(p);
}
free(wk);
return i;
}
int main(void){
int column, len, word_count;
int i, spaces, between, remain;
size_t count_char;
char text[WIDTH + 1];
char *p = text;
printf("Enter the width of the column: ");scanf("%d%*c", &column);
printf("Enter a line of text: ");scanf("%" str(WIDTH) "[^\n]", text);
len=strlen(text);
if(len > column || len > WIDTH){
fprintf(stderr, "too long text!\n");
return -1;
}
ruler_print(WIDTH);
word_count = count_word(text, &count_char);
spaces = column - count_char;
between = spaces / (word_count -1);
remain = spaces - (word_count -1)*between;
strtok(text, " ");
for(i=0;i<word_count-1;++i){
printf("%s%*s", p, between + (remain ? 1 : 0), " ");
if(remain) --remain;
p=strtok(NULL, " ");
}
printf("%s\n", p);
return 0;
}