How to output a marquee string that is smaller than the size of the marquee sign?

自闭症网瘾萝莉.ら 提交于 2020-01-11 13:20:10

问题


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]

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  ]

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

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]

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?


回答1:


Change the loop into this:

    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");
        }
    }


来源:https://stackoverflow.com/questions/35678399/how-to-output-a-marquee-string-that-is-smaller-than-the-size-of-the-marquee-sign

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!