c - Thermo 迷你打印机上 C 语言的文本换行

标签 c twitter word-wrap maxlength

我真的很想为我的一个项目提供一些帮助。我是一名平面设计学生,几乎没有编程经验。我为热迷你打印机创建了一个程序,可以根据使用的特定主题标签识别 Twitter 上发布的推文并自动打印它们。

但是,它基于 32 个字符的行长度,并将单词分成两半,而不是将整个单词移动到另一行。我的一个 friend 建议自动换行,但我在网上找不到任何可以帮助我的东西,而且我发现的大多数代码往往适用于 c++ 或 c#。

到目前为止的代码可以在下面找到:

// Build an ArrayList to hold all of the words that
// we get from the imported tweets

ArrayList<String> words = new ArrayList();
Twitter twitter;
import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

void setup() {
    String portName = Serial.list()[0];
    myPort = new Serial(this, portName, 9600);
    //Set the size of the stage, and the background to black.
    size(550,550);
    background(0);
    smooth();

    //Make the twitter object and prepare the query
    twitter = new TwitterFactory(cb.build()).getInstance();
}

void draw() {

    Query query = new Query("#R.I.P");
    query.setRpp(1);

    //Try making the query request.
    try {
        QueryResult result = twitter.search(query);
        ArrayList tweets = (ArrayList) result.getTweets();

        for (int i = 0; i < tweets.size(); i++) {
            Tweet t = (Tweet) tweets.get(i);
            String user = t.getFromUser();
            String msg = t.getText();
            Date d = t.getCreatedAt();
            println("Tweet by " + user + " at " + d + ": " + msg);
            msg = msg.replace("\n"," ");
            myPort.write(msg+"\n");
        };
    }
    catch (TwitterException te) {
        println("Couldn't connect: " + te);
    };
    println("------------------------------------------------------");
    delay(20000);
}

最佳答案

既然你有线的长度,那么这并不难......

迭代字符串,一次一个字符。如果您看到空格,请将位置保存为例如last_space。如果您的迭代超过了最大行长度,则转到 last_space 位置并将空格转换为换行符,将位置计数器重置为零,然后从该位置重新开始。

<小时/>

也许可以像这样实现它:

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

#define LINE_LENGTH 32

char text[256] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ac risus elit, id pellentesque magna. Curabitur tempor rutrum enim, sit amet interdum turpis venenatis vel. Praesent eu urna eros. Mauris sagittis tempor felis, ac feugiat est elementum sed. Praesent et augue in nibh pharetra egestas quis et lectus. Lorem ipsum.";

static void wrap(char *text, const int length)
{
    int last_space = 0;
    int counter = 0;

    for (int current = 0; text[current] != '\0'; current++, counter++)
    {
        if (isspace(text[current]))  // TODO: Add other delimiters here
            last_space = current;

        if (counter >= length)
        {
            text[last_space] = '\n';
            counter = 0;
        }
    }
}

int main(void)
{
    printf("Before wrap:\n%s\n", text);

    wrap(text, LINE_LENGTH);

    printf("\nAfter wrap:\n%s\n", text);

    return 0;
}

重要说明:wrap 函数修改作为 text 参数传入的缓冲区。这意味着它无法处理文字字符串,因为它们是只读的。它的工作原理是用换行符替换空格。如果您修改函数以添加多个字符,那么更好的解决方案是(重新)分配一个足够大的新缓冲区来容纳修改后的字符串。

关于c - Thermo 迷你打印机上 C 语言的文本换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15720006/

相关文章:

html - 如何在没有转推的情况下嵌入推特时间线?

python - Twitter 主题标签搜索结果 - 超过 7 天

html - 尽管 block 中有空格,为什么在连续 block 之间发生换行?

c - 在 c 中清除输入缓冲区的替代方法

c - 检查有效 Unix FTP 命令的程序(不工作)

c - gcc 编译器选项是什么? (-lGL -lGLU -lglut)

java - 在java中获取包装对象

在c中不使用任何第三方库创建pdf文件?

java - Twitter 用户中的default_profile_image

android - Android TextView不换行特殊字符