user2194374
user2194374

Reputation: 123

Add space to a string

I am trying to add a space to each space until column = 0. I am not sure how to do this.

The problem is the following. If you look at a newspaper you will see that the writing is justified to fit into the columns. Write a program that reads in the width of the columns in a newspaper and then a line of text. Justify the line of text to fit into a column of that width. When your program is running, the screen should look something like this:

Enter the width of the column: 40
Enter a line of text: Good morning how are you?
12345678901234567890123456789012345678901234567890...
Good     morning     how     are    you?

The justification is done by counting the number of gaps in the text. In the above example, there are 4 gaps. Then each gap must have spaces added to it. The number of extra spaces must be shared out as evenly as possible. In the above example, the first three gaps have 5 spaces each and the last gap has 4 spaces.

Notes:

  1. If the text is longer than the column then you must report an error – don't try and break it into two lines!
  2. Assume that the text will have more than one word in it.
  3. Note the header line consisting of 123456789012345678.... this is useful to check your result. You can make this header line as long as you like – 70 spaces would be a useful length.

Thanks

#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;
}

Upvotes: 3

Views: 13834

Answers (2)

kundrata
kundrata

Reputation: 679

This is what i made. It's far from ideal, but you get the point. You just need to put in conditions, like when the string entered is larger or equal than 40 chars, to skip the procedure.

#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;
}

Upvotes: 2

BLUEPIXY
BLUEPIXY

Reputation: 40145

#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;
}

Upvotes: 0

Related Questions