Replace space with newline

I'm trying to do a space with line replacer for Ti-89 calculators so that I can print lines without them being cut because of the horizontal character length. They would normally look like so

This is a pretty long test
ing for the Ti89 Calculator

and I would like them to look like so

 This is a pretty long
 testing for the Ti89
 Calculator

I tried to do it with this code

void _print_line(char* string)
{
        int k = strlen(string);
        if(k > 26)
        {
                int n = 0;
                int c = 25;
                while(n == 0)
                {
                        if(string[c] == 32)
                        {
                                n = 1;
                        }
                        else
                        {
                                c--;
                        }
                        if(c <= 0)
                        {
                                n = 2;
                        }
                }
                if(n == 1)
                {
                        string[c] == '\n';
                }
        }
        printf("%s\n", string);
}

But it seems to just ignore it and keep printing it like the first example.

Upvotes: 1

Views: 655

Answers (3)

BLUEPIXY
BLUEPIXY

Reputation: 40145

There needs to be processed for the entire string.

Keep a record of the current output character position to check whether or not exceed the specified width when you output the next word.
like this:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define DISP_OUT   stdout
#define DISP_WIDTH 26

int disp_one(char ch){
    static int pos;
    fputc(ch, DISP_OUT);
    if(ch == '\n')
        pos = 0;
    else
        ++pos;
    if(pos == DISP_WIDTH){
        fputc('\n', DISP_OUT);
        pos = 0;
    }
    return pos;
}

typedef enum word_break {
    KEEP, BREAK
} WORD_BREAK;

void disp(const char *str, WORD_BREAK word_break){
    static int pos;

    switch(word_break){
    case BREAK:
        while(*str){
            pos = disp_one(*str++);
        }
        break;
    case KEEP:
        while(*str){
            if(isspace((unsigned char)*str)){
                pos = disp_one(*str++);
                continue;
            }
            const char *end = str;//end : word end (find delimiter)
            while(*end && !isspace((unsigned char)*end))
                ++end;

            int len = end - str;//length of next output word
            if(pos + len >= DISP_WIDTH && len < DISP_WIDTH){
                pos = disp_one('\n');
            }
            while(str < end){
                pos = disp_one(*str++);
            }
        }
        break;
    }
}

int main(void){
    char *text = "This is a pretty long testing for the Ti89 Calculator";

    disp(text, BREAK);
    disp("\n", BREAK);
    disp(text, KEEP);
    return 0;
}

Upvotes: 0

javey
javey

Reputation: 494

As Till said, you are not inserting the carriage return. The line

string[c] == '\n';

needs to be

string[c] = '\n';

With the difference being one "equals" sign versus two.

The reason is because "==" is a conditional operator which evaluates to either true or false, while "=" is the assignment operator which sets a value to a variable.

Upvotes: 0

Till
Till

Reputation: 27597

You are not inserting the carriage return.

Replace string[c] == '\n';

With string[c] = '\n';

Upvotes: 3

Related Questions