如何输出一个选取框字符串是超过选取框标志的尺寸变小? [英] How to output a marquee string that is smaller than the size of the marquee sign?

查看:152
本文介绍了如何输出一个选取框字符串是超过选取框标志的尺寸变小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的code发生在字母串,然后输出在选取框时尚的字符串,标志是5.因此,例如,如果我想输出的Hello World!,输出的大小是:

So my code takes in a string of letters, then outputs that string in a marquee fashion with the size of the sign being 5. So for example, If I want to output "Hello World!", the output is:

[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]

但问题是,如果有字母串的长度为8和字幕标志的大小为10,那么我想选取框内标志只显示字符串一次。所以,如果一个字符串的大小,它比显示字幕标志更小,只显示该字符串一次。例如:

But the problem is, if there is a string of letters that has length 8 and the size of the marquee sign is 10, then I want to just display the string once within the marquee sign. So if a string has a size that's smaller than the indicated marquee sign, just display the string once. For example:

Input: 
Activist (the string that I want to output in a sign)
10 (the length of the sign)
Output:
[Activist  ]

注意如何仍有10位大帐篷标志,它所做的只是输出字符串本身一次。

Notice how there are still 10 spaces in the marquee sign and all it does is simply output the string by itself once.

下面是我的code。我把它提出来运行不止一次如果给出:

Here is My code. I have it made to run more than once if indicated:

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

void ignoreRestOfLine(FILE *fp) {
    int c;
    while ((c = fgetc(fp)) != EOF && c != '\n');
}

int main(void) {
    int num_times, count = 0;
    int marq_length, sign = 0;
    scanf("%d ", &num_times);
    char s[100];
    for (count = 0; count < num_times; count++) {
        if (fgets(s, sizeof(s), stdin) == NULL) {
            // Deal with error.
        }    
        if (scanf("%d", &marq_length) != 1) {
           // Deal with error.
        }

        ignoreRestOfLine(stdin);

        size_t n = strlen(s) - 1;
        int i, j;

        if (s[strlen(s) - 1] == '\n')
            s[strlen(s) - 1] = '\0';

        printf("Sign #%d:\n", ++sign);

        for (i = 0; i < n + 1; i++) {
            putchar('[');
            for (j = 0; j < marq_length; j++) {
                char c = s[(i + j) % (n + 1)];
                if (!c)
                    c = ' ';
                putchar(c);
            }
            printf( "]\n" );
        }
    }
}

此输入和输出如下:

The Input and Output for this is as follows:

Input:
3
Hello World!
5
Sign #1: (This is the output)
[Hello]
[ello ]
[llo W]
[lo Wo]
[o Wor]
[ Worl]
[World]
[orld!]
[rld! ]
[ld! H]
[d! He]
[! Hel]
[ Hell]
Activist
10
Sign #2: (This is the output)
[Activist A]
[ctivist Ac]
[tivist Act]
[ivist Acti]
[vist Activ]
[ist Activi]
[st Activis]
[t Activist]
[ Activist ]
LOL
2
Sign #3: (This is the output)
[LO]
[OL]
[L ]
[ L]

一切都是正确的,除了标志#2。如何在选取框标志我只输出字符串一次如果字符串的长度小于选取框标志的大小?

Everything is right except for sign #2. How do I output just the string once in the marquee sign if the length of the string is less than the marquee sign size?

推荐答案

循环切换到该

    if (n <= marq_length) {
        printf("[%-*s]\n", marq_length, s);
    } else {
        for (i = 0; i < n + 1; i++) {
            putchar('[');
            for (j = 0; j < marq_length; j++) {
                char c = s[(i + j) % (n + 1)];
                if (!c)
                    c = ' ';
                putchar(c);
            }
            printf("]\n");
        }
    }

这篇关于如何输出一个选取框字符串是超过选取框标志的尺寸变小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆