简体   繁体   English

格式化文本的C程序

[英]C program to format text

I am given a string with a lot of spaces between words.我得到一个字符串,单词之间有很多空格。 I must write a program that transforms the given string into a text, with each line having no more than 80 characters.我必须编写一个程序,将给定的字符串转换为文本,每行不超过 80 个字符。 No word should be split and justify must be used.不应拆分任何词,必须使用 justify。 No additional libraries or functions to be used!无需使用额外的库或函数! I need help finishing the problem.我需要帮助解决这个问题。

Example input: "John     had  a lot          of work to do."
Result:"John had
        a lot of
        work  to
              do"

In the example obviously I didn't use the 80 charaters rule, but rather 8. My code this far, eliminates the extra spaces and can calculate the length of the string.在示例中,显然我没有使用 80 个字符规则,而是使用 8 个字符。到目前为止,我的代码消除了额外的空格并可以计算字符串的长度。

#include <stdio.h>

int main()
{
   char text[1000], blank[1000],rez[1000];
   int n,i;
   printf("give string\n");
   gets(text);
   blankremove(text,blank);
   printf("%s\n",blank);
   n=lenght(blank);
   printf("%d", n);

   return 0;
}

int lenght(char a[]){
int lenght;
lenght=0;
while (a[lenght]!='\0')
{
    lenght++;
}
return lenght;
}

int blankremove(char text[], char blank[])
{

   int c = 0, d = 0;
   while (text[c] != '\0') {
      if (text[c] == ' ') {
         int temp = c + 1;
         if (text[temp] != '\0') {
            while (text[temp] == ' ' && text[temp] != '\0') {
               if (text[temp] == ' ') {
                  c++;
               }
               temp++;
            }
         }
      }
      blank[d] = text[c];
      c++;
      d++;
   }
   blank[d] = '\0';}

(This sounds like homework to me. Keep in mind the teacher has access to Stackoverflow, as well.) (这对我来说听起来像是家庭作业。请记住,老师也可以访问 Stackoverflow。)

Let's see, your formatting is ... regrettable but that is not what you are asking.让我们看看,您的格式是...令人遗憾,但这不是您要问的。

I think this will do what you need.我认为这将满足您的需求。

Add these lines just before the return in main:在 main 中的 return 之前添加这些行:

fillLine(blank,rez,sizeof(rez));
printf("%s\n", rez);

Then create a function called fillLine that will look to see what will fit and put it on the line, if it will fit.然后创建一个名为 fillLine 的函数,该函数将查看适合的内容并将其放在线上(如果适合)。

/* Find the length of the next word on the line, upto the next space.
 */
int lenWord(char *in)
{
    int ii;
    for(ii=0; in[ii]!=0 && in[ii]!=' '; ii++);
    return(ii);
}
#define MAX_COLUMNS 16
/*
 * This will stuff what it can in MAX_COLUMNS columns
 */
int fillLine(char *blank, char *rez, int rezSize)
{
    int in;
    int out;
    int col;
    for(col=0, in=0, out=0; blank[in]!=0 && out<rezSize;) {
        int len=lenWord(&blank[in]);
        if(col+len+1 < MAX_COLUMNS ) {
            int ii;
            for(ii=0; ii<len; ii++) {
                rez[out]=blank[in];
                in++;
                out++;
                col++;
            }
            rez[out]=' ';
            in++;
            out++;
            col++;
        } else {
            rez[out]='\n';
            out++;
            col=0;
        }
    }
    return(out);
}

This version has a few problems (that I suggest you resolve before turning it in):这个版本有一些问题(我建议你在提交之前解决):

  1. I passed the size of the output rez area to the function but don't check it enough to make sure I am not going beyond the end and trashing something else.我将输出rez区域的大小传递给函数,但没有对其进行足够的检查,以确保我不会超出结尾并破坏其他东西。
  2. I leave a blank at the end of all the lines which means they are not as filled as they might be.我在所有行的末尾留了一个空白,这意味着它们没有像它们可能的那样填充。
  3. The fillLine function could be combined with your blankRemove function to make the program more concise. fillLine函数可以与您的blankRemove函数结合使用,使程序更加简洁。
  4. Some people prefer to put the constants on the left in the equality check in case there is a typo: 0!=in[ii] not in[ii]!=0 .有些人更喜欢将常量放在等式检查的左侧,以防出现拼写错误: 0!=in[ii]而不是in[ii]!=0 This helps to avoid the potential for if( in[ii]=0) { .这有助于避免if( in[ii]=0) {的可能性。 Some people get excited about Yoda Conditions , both for and against.有些人对尤达条件感到兴奋,无论是支持还是反对。

This will give you the idea.这会给你的想法。 I might comment that using variables like ii rather than i makes it easier to search for them in the text editor.我可能会评论说,使用 ii 之类的变量而不是 i 可以更轻松地在文本编辑器中搜索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM