3

I am trying to write a program to insert spaces between words to fit a column, for example:

  • You read in a line of text, eg: Good morning how are you?
  • You read in the width of the column, eg: 40.
  • Then I have counted how many spaces there are in this text (4).
  • Now I need to distribute the remaining spaces in between these words so the length of the text is 40.

For example:

Good     morning     how     are    you?
1234567890123456789012345678901234567890

My problem comes when I try to insert the spaces in between words as I don't know how to do this. This is what I have so far.

#include <stdio.h>
#include <string.h>
char text[65], spaces[50], ch;
int i, remainder, spacesr, count, column, length, distribution;
int main(){
    i = 0;
    count = 0;
    printf("Please enter a line of text: ");
    while(ch != '\n')
    {
        ch = getchar();
        text[i]=ch;
        i++;
    }
    text[i]='\0'; 
    printf("Text: %s",text);
    printf ("Please enter the width of the column: ");
    scanf ("%d", &column);
    for (i = 0; text[i] != '\0'; i++) {
        if (text[i] == ' ') {
            count++;
        }
    }
    length = strlen(text);
    spacesr = column - length;
    distribution = spacesr / count;
    remainder = spacesr % count;
    if (length > column) {
        printf ("ERROR: Length of text exceeds column width.");
    }
}

I have calculated the amount of spaces in the read in text, then calculated the amount of spaces I would have remaining, then divided that by the amount of spaces to determine how many spaces I need to put between each word. The remainder of these spaces will be distributed evenly after the main spaces have been entered.

What do you mean by main spaces?

Basically I want to fit the phrase "Good morning how are you?" to a column 40 characters wide by adding spaces between words. Is it possible to do something like this:

for (i = 0; text[i] != '\0'; i++) {
    if (text[i] == ' ') {
    then add a certain amount of spaces to it
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
J. Doe
  • 31
  • 1
  • 1
  • 3
  • what do you mean by main spaces? – shafeeq May 18 '16 at 03:52
  • 1
    Please note that all the variables in the program should be local to `main()`; not one of them needs to be a global variable. You should avoid globals as much as possible. They're sometimes necessary and the best way to deal with a situation. However, in the ordinary course of coding, variables should be local to the function where they're used. – Jonathan Leffler May 18 '16 at 04:01
  • It seems that you can't use the *string.h* headers, is that correct? – Iharob Al Asimi May 18 '16 at 04:03
  • What happens if the user types several spaces between a word? How do you calculate how many spaces you need to add? Do you simply need to print the information, or do you need to make an array that can be printed? What happens if the user specifies 40 but enters 50 characters? What happens if they enter 128 characters? Given that the N words add up to L characters, plus N-1 spaces, you need to add W - (N - 1) - L spaces more or less evenly to the result? How are you going to do that? – Jonathan Leffler May 18 '16 at 04:06
  • 1
    @iharob: the code includes `` and uses `strlen()` — those suggest that the functions can be used. – Jonathan Leffler May 18 '16 at 04:07
  • By main spaces I mean the spaces that arent a remainder. And we assume that the user types in one space between each word. If it was a column of 10 and the string was 6 characters long (lets just say the string was "hey hi" there would need to be 4 spaces added to the middle of them. – J. Doe May 18 '16 at 04:10
  • How can I use strtok to split the string into seperate words, then add a read in amount of spaces after each word? – J. Doe May 18 '16 at 04:25
  • Do you know how to handle arrays of pointers? – Jonathan Leffler May 18 '16 at 05:16
  • How can you split up the words in my string into separate words? – J. Doe May 18 '16 at 05:27

2 Answers2

0

You need to break your input string into separate words. Take a look at the answers to this earlier question on StackOverflow for some techniques that you can use for that.

After that, it's just a matter of emitting the separate words with the correct number of spaces in between.

Community
  • 1
  • 1
Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
  • 2
    This sounds more like a comment. PS. I did not downvote. – R Sahu May 18 '16 at 04:03
  • It's not a comment. This question looks like a homework problem. As per the FAQ: "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it." The difficulty was not described here, so I inferred it from what is not present in the code sample. I don't write code for homework questions. I suggest ways to think about the problem to help the questioner understand how to break it down the way an experienced developer would, and I point to code in existing answers when appropriate. – Richard Schwartz May 18 '16 at 04:27
  • If a question is not clear, vote to close it, downvote it. Don't answer it with a poor quality answer. – R Sahu May 18 '16 at 04:29
  • I respectfully disagree on both counts. – Richard Schwartz May 18 '16 at 04:32
  • 1
    That said, I would prefer that homework questions were not welcomed here on StackOverflow at all, but they are. – Richard Schwartz May 18 '16 at 04:34
0

One of the many ways to do it.

Suppose totalSpace is the buffer length in which we have to fit the string. and str = the original string we have.

Algo:

int extraSpace = totalSpace - strlen(str);
int numWords = findWordsInString(str);
int numSpaces = numWords - 1;
int incrementEachSpaceby = extraSpace/numSpace;

//Now linear scan of str and increase spaces by value of incrementEachSpaceby 

    char *newStr = malloc(totalspace);
    int i =0, int j = 0;
    int k;
    while (i < strlen(str) && j < totalspace)
    {
        while (str[i] != ' ') {
            newStr[j++] = str[i++];
        }

        while (str[i] == ' ')
            newStr[j++] = str[i++];

        k = incrementEachSpaceby;

        while (k) { 
            newStr[j++] = ' ';
            k--;
        }
    }

This is just a superficial idea. you can improve it further.

Ritesh
  • 1,809
  • 1
  • 14
  • 16