C-适合固定宽度的琴弦

时间:2018-11-27 17:21:23

标签: c string

我想将一个字符串放入固定宽度的多行中。我设法将字符串分成不同的行,以使它们的长度不会超过固定宽度,但是问题是某些行的字符长度不是 width 80 )个字符,这就是为什么我试图通过在单词之间添加空格来分布extra_space的原因。

#include <stdio.h>
#include <string.h>
#include "stringdefault.h"
#include <conio.h>
#include <stdlib.h>



int main(){
    int     width = 80;
    char s1[10000];
    char substring[100] = " ";
    char space = ' ';
    gets(s1);
    removeSpaces(s1);
    char *base,*right_margin;
    int extraSpace, numWords, numSpaces, incrementEachSpaceby, ind1, ind2, k;
    int length;

    length = string_length(s1);
    base = s1;
    for(int i = 0; i < width; i++)
    {
        printf("%d", i%10);
    }
    printf("\n");

    while(*base)
    {
        if(length <= width)
        {
            puts(base);     // display string
            return(0);       //and leave
        }
        right_margin = base+width;
        while(!isspace(*right_margin))
        {
            right_margin--;
            if( right_margin == base)
            {
                right_margin += width;

                while(!isspace(*right_margin))
                {
                    if( *right_margin == '\0')
                        break;
                    right_margin++;

                }
            }
        }
        *right_margin = '\0';
        if(string_length(base) < width)
        {
            char *newStr = malloc(width);
            extraSpace = width - string_length(base);
            numWords = numberOfWords(base);
            numSpaces = numWords - 1;
            incrementEachSpaceby = extraSpace/numSpaces;
            ind1 = 0;
            ind2 = 0;
            while (ind2 < width)
            {
                newStr[ind2] = base[ind1];

                ind1++;
                ind2++;
            }
            for(int i = 0; newStr[i]!='\0'; i++)
                if((isspace(newStr[i])))
                    {
                        if(extraSpace > 0)
                            k = extraSpace;
                        else
                            k = 1;
                        while(k)
                        {
                            insert_substring(newStr, substring, i);
                            k--;
                        }
                    }
            puts(newStr);
        }
        else
            puts(base);
        length -= right_margin-base+1;      // +1 for the space
        base = right_margin+1;
    }
    return 0;
}

stringdefault.h

int string_length(char s[])
{
    int length = 0;
    for(int i=0; s[i]!='\0'; i++)
        length++;
    return length;
}

char *substring(char *string, int position, int length)
{
   char *pointer;
   int c;

   pointer = malloc(length+1);

   if( pointer == NULL )
       exit(EXIT_FAILURE);

   for( c = 0 ; c < length ; c++ )
      *(pointer+c) = *((string+position-1)+c);

   *(pointer+c) = '\0';

   return pointer;
}



void insert_substring(char *a, char *b, int position)
{
   char *f, *e;
   int length;

   length = strlen(a);

   f = substring(a, 1, position - 1 );
   e = substring(a, position, length-position+1);

   strcpy(a, "");
   strcat(a, f);
   free(f);
   strcat(a, b);
   strcat(a, e);
   free(e);
}

char *removeSpaces(char *str)
{
  int  ip_ind = 0;
  char *ptr;
  while(*(str + ip_ind))
  {
    if ( (*(str + ip_ind) == *(str + ip_ind + 1)) && (*(str + ip_ind)==' ') )
    {
        ptr = str + ip_ind+1;

        do{
           *(ptr-1) = *ptr;
        }while(*ptr++ != '\0');


    }
    else
    ip_ind++;
  }
  *(str + ip_ind) = '\0';

 return str;
}

The output that I get without the while loop inside the if statement

似乎newStr字符集在第一个字符之前包含空格

0 个答案:

没有答案