0

problem After receiving the string S, write a program that repeats each character R times to create a new string and print it out. That is, you can make P by repeating the first character R times and repeating the second character R times. S contains only the QR Code "alphanumeric" characters.

QR Code "alphanumeric" character is 0123456789ABCDEFGHIJK

Input The number T (1 ≤ T ≤ 1,000) of test cases is given in the first line. Each test case is given by dividing the number of repetitions R (1 RR 88) and the string S into spaces. The length of S is at least 1 and does not exceed 20 characters.

Output Output P for each test case.

input Example

2
3 ABC
5 /HTP

output Example

AAABBBCCC
/////HHHHHTTTTTPPPPP

My code:

#include<stdio.h>
int main() {
    int a=0;
    scanf("%d", &a);
    for (int k = 0; k < a; k++) {
        int d;
        char b[20];
        scanf("%d", &d);
        scanf("%s", &b);
        for (int i = 0; b[i]!=NULL; i++) {
            for (int j = 0; j < d; j++) {
                printf("%c", b[i]);
            }
        }
    }
}
  • 2
    You're not printing a newline at the end of each test case. `b` is too small. If the string can be 20 characters long you need to allocate at least 21 to account for the terminator. – Retired Ninja Apr 14 '22 at 03:39
  • The `5 /HTP` example seems to contradict the 'QR Code alphanumeric characters' requirement. What gives? – Jonathan Leffler Apr 14 '22 at 04:24
  • 2
    Lesson -- *Don't Skimp On Buffer Size*. `scanf("%s", &b)` is worse than `gets(b)`. See: [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) You **must** provide a *field-width* modifier when using `scanf()` with `"%s"`, e.g. `scanf ("%19s", b);`. – David C. Rankin Apr 14 '22 at 04:25
  • 1
    I'm not sure what the specification means when it says "Each test case is given by dividing the number of repetitions R (1 RR 88) and the string S into spaces". Can you clarify? – Jonathan Leffler Apr 14 '22 at 04:26

2 Answers2

1

There is a problem in the code:

scanf("%s", b);

we write "b" instead of "&b"

‘&’ is used to get the address of the variable. C does not have a string type, String is just an array of characters and an array variable stores the address of the first index location.By default the variable itself points to the base address and therefore to access base address of string, there is no need of adding an extra ‘&’

so we can write :

#include<stdio.h>
int main() {
    int a=0;
    scanf("%d", &a);
    for (int k = 0; k < a; k++) {
        int d;
        scanf("%d", &d);
        char b[20];
        scanf("%s",b);
        for (int i = 0; b[i]; i++) {
            for (int j = 0; j < d; j++) {
                printf("%c", b[i]);
            }
        }
       printf("\n");
    }
}
TheKing
  • 136
  • 9
0

Improvements

  • Use size_t to iterate through arrays
  • NOTE: char b[20];, b decays to pointer (sizeof() operator is an exception)
  • In line scanf("%s", &b);, &b is not required it will cause undefined behavior, just b is fine
  • Always check whether scanf() input was successful
  • Don't use "%s", use "%<WIDTH>s", to avoid buffer-overflow
  • b[i]!=NULL is quite wrong, NULL is a pointer whereas b[i] is a char, and char can't be compared with pointer, you should check for '\0' or just 0
  • Initialize your variable b using = {}, then all the elements of b will be 0
  • Length of b should be 21 +1 for the null terminating character

Final Code

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int a = 0;
    if (scanf("%d", &a) != 1) {
        fputs("bad input", stderr);
        return EXIT_FAILURE;
    }
    for (int k = 0; k < a; k++) {
        int d;
        if (scanf("%d", &d) != 1) {
            fputs("bad input", stderr);
            return EXIT_FAILURE;
        }
        char b[21] = {};
        if (scanf("%20s", b) != 1) {
            fputs("bad input", stderr);
            return EXIT_FAILURE;
        }
        for (size_t i = 0; b[i] != 0; i++) {
            for (int j = 0; j < d; j++) {
                printf("%c", b[i]);
            }
        }
        puts("\n");
    }
    return EXIT_SUCCESS;
}
Input
2
3 ABC
5 /HTP
Output
AAABBBCCC

/////HHHHHTTTTTPPPPP

TRY IT ONLINE

Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23