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

Time:04-14

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

CodePudding user response:

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 nul terminating character

Final Code

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

int main(void) {
    int a = 0;
    if(scanf("%d", &a) != 1)
    {
        perror("bad input");
        return EXIT_FAILURE;
    }
    for (int k = 0; k < a; k  )
    {
        int d;
        if(scanf("%d", &d) != 1)
        {
            perror("bad input");
            return EXIT_FAILURE;
        }
        char b[21] = {};
        if(scanf(" s", b) != 1)
        {
            perror("bad input");
            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

CodePudding user response:

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");
    }
}
  •  Tags:  
  • c
  • Related