김지원
김지원

Reputation: 1

I don't know why I got it wrong in my code. c language, string iterations

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

Upvotes: 0

Views: 105

Answers (2)

Darth-CodeX
Darth-CodeX

Reputation: 2662

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

Upvotes: 0

TheKing
TheKing

Reputation: 146

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

Upvotes: 1

Related Questions