首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向字符串中添加空格

向字符串中添加空格
EN

Stack Overflow用户
提问于 2013-05-01 10:02:34
回答 2查看 13.1K关注 0票数 3

我试图在每个空间中添加一个空格,直到column = 0。我不知道该怎么做。

问题如下。如果你看一份报纸,你就会发现这篇文章是合情合理的。编写一个程序,读取报纸上列的宽度,然后是一行文本。对文本行进行对齐,使之与该宽度的列相匹配。当程序运行时,屏幕应该如下所示:

代码语言:javascript
运行
复制
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个空格。

备注:

  1. 如果文本比列长,则必须报告错误--不要尝试将其拆分为两行!
  2. 假设文本中包含多个单词。
  3. 注意由123456789012345678组成的标题行.这对于检查您的结果非常有用。你可以让这个标题行,只要你愿意-70个空格将是一个有用的长度。

谢谢

代码语言:javascript
运行
复制
#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;
}
EN

回答 2

Stack Overflow用户

发布于 2013-05-01 11:11:13

这就是我做的。这远非理想,但你明白重点。您只需要输入条件,比如输入的字符串大于或等于40个字符,就可以跳过这个过程。

代码语言:javascript
运行
复制
#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;
}
票数 2
EN

Stack Overflow用户

发布于 2013-05-01 11:52:54

代码语言:javascript
运行
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16316144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档