0

My Code :

#include <stdio.h>

int main()
{

    int size,i;

    printf("Enter the size of the string: ");
    scanf("%d",&size);
    int arr[size];
    printf("Enter the String: ");
    for(i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("The string you've entered is: ");
    for(i=0;i<size;i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

I've tried to enter an array of numbers like: 10234, but the next statement is not getting executed, it's not showing me the printf statement i.e "The string you've entered is: ".

But it's working if I enter the array of numbers like: 1 0 2 3 4 (with spaces). How can I make it work without spaces too, can you help me with it ?.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 2
    The `%d` format specifier for `scanf` reads a whole integer, not separate digits. If you want to read separate digits then perhaps read character by character instead? – Some programmer dude Aug 12 '22 at 05:47
  • 4
    Put a `printf()` in the first `for()` loop to see what `scanf()` has delivered to you... – Fe2O3 Aug 12 '22 at 06:05
  • 1
    To read one digit at a time, you could use `"%1d"`. But if you enter `10234`, it isn't clear which digits are meant to be the size and which are meant to be the values. – Jonathan Leffler Aug 12 '22 at 06:18
  • 1
    I am with your programs behaviour "10234" is a single number and "1 0 2 3 4" is five numbers. What else do you expect with a "there will be a number" `"%d"` format string? Scanf is (slightly) better than its reputation, it does offer powerful control over what is expected. In the specification, which you seem not to have thoroughly enough studied. Try https://en.cppreference.com/w/c/io/fscanf – Yunnosch Aug 12 '22 at 06:52
  • Meanwhile you understood that **10234** is one single number, you want to enter digits. That is input as string or single char and converting char to int. – Joop Eggen Aug 12 '22 at 12:49

4 Answers4

0

If you want to read string and convert digits to the int array:

size_t getsize(FILE *fi)
{
    char buff[20];
    size_t result = 0;
    if(fgets(buff, sizeof(buff), fi))
    {
        if(sscanf(buff, "%zu", &result) != 1) result = 0;
    }
    return result;
}


int main()
{

    size_t size,i;

    printf("Enter the size of the string: ");
    if(!(size = getsize(stdin))) goto error_exit;

    do{
        char str[size + 2];
        int arr[size];
        printf("Enter the String: ");
        if(!fgets(str, size + 2, stdin)) goto error_exit;

        for(i=0;i<size;i++)
        {
            if(str[i] == '\n') break;
            if(!isdigit((unsigned char)str[i])) goto error_exit;
            arr[i] = str[i] - '0';
            printf("%d ",arr[i]);
        }
        return 0;
    }while(0);
    error_exit:
    printf("Error!!!\n");
    return 1;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0
#include <stdio.h>

int main()
{

    int size,i;

    printf("Enter the size of the string: ");
    scanf("%d",&size);
    char arr[size];
    printf("Enter the String: ");
    for(i=0;i<=size;i++)
    {
        scanf("%c",&arr[i]);
    }
    printf("The string you've entered is: ");
    for(i=0;i<=size;i++)
    {
        printf("%c ",arr[i]);
    }
    return 0;
}
0

Use scanf("%1d", &arr[i]); (1 added) to limit input to 1 non-white-space character.

Also better to check the return value of all scanf() calls.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

This code adds some plausible checking and uses "%1d" for the scanf() input format for the subsequent entry of the digits.

#include <stdio.h>

int main(void)
{
    int size;

    printf("Enter the size of the string: ");
    if (scanf("%d", &size) != 1 || size <= 0 || size > 1024)
    {
        fprintf(stderr, "failed to read a valid size between 1 and 1024\n");
        return 1;
    }
    int arr[size];
    printf("Enter the String: ");
    for (int i = 0; i < size; i++)
    {
        if (scanf("%1d", &arr[i]) != 1)
        {
            fprintf(stderr, "failed to read entry %d\n", i+1);
            return 1;
        }
    }

    printf("The string you've entered is: ");
    int length = 0;
    const char *pad = "";
    for (int i = 0; i < size; i++)
    {
        length += printf("%s%d", pad, arr[i]);
        pad = " ";
        if (length > 60)
        {
            length = 0;
            putchar('\n');
            pad = "";
        }
    }
    if (length > 0)
        putchar('\n');
    return 0;
}

The code was the source file scan61.c and compiled to scan61. Sample outputs:

$ scan61
Enter the size of the string: 13
Enter the String: 12342234323442341
The string you've entered is: 1 2 3 4 2 2 3 4 3 2 3 4 4
$ scan61
Enter the size of the string: 17
Enter the String: 1234 2234 32 34 4 2 3 4 
1
The string you've entered is: 1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4 1
$ scan61
Enter the size of the string: 4
Enter the String: -1 -2 -3 -4
failed to read entry 1
$

The first sample shows that extra digits can be entered. The second shows that although the scanf() reads single digits, they can be separated by spaces or newlines — the scanf() family of functions is not appropriate for use when you need line-based inputs. The third shows that you cannot apply a sign to the entered numbers.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278