0

I have problem that when I run my program it does just take for the array everything that is on first input instead of second one and I just cant figure out why. To specify if i write in console 1 letter it does first part ok, but then print this weird ╠╠╠╠╠╠╠╠╠ (lot of it) and second task then just take this like input. This task is supposed to take string (even with spaces) and count bid/small letters, numbers and other. Can somebody pls tell me what I did wrong? Thanks for help and stay safe

#include<stdio.h>

#include <string.h> 

int main()
{

    char z;
    int B = 0, s = 0, n = 0, else = 0;          
    printf("Enter character:");
    scanf_s("%c", &z, 1);
    if (z >= 'A' && z <= 'Z')
        printf("Big letter\n");
    else if (z >= 'a' && z <= 'z')
        printf("small letter\n");
    else if (z >= '0' && z <= '9')
        printf("number\n");
    else
    {
        printf("else\n");
    }
    

    char something[50];
    printf("Enter string:");
    scanf_s("%50[^\n]", str, 50);
    printf("Your name is %s", something);
    int space = 0;
    int len = 0;
    for (int i = 0; something[i] != '\0'; i++)
    {
        if (something[i] == ' ')
        {
            space++;
        }
        else
        {
            len++;
        }
    }
    for (int i = 0; i <= len; i++)
    {
        if (something[i] >= 'A' && something[i] <= 'Z')
        {
            B++;
        }
        else if (something[i] >= 'a' && something[i] <= 'z')
        {
            s++;
        }
        else if (something[i] >= '0' && something[i] <= '9')
        {
            n++;
        }
        else
        {
            other++;
        }


    }
    printf("Your input %s contains:\nB=%d s=%d n=%d else=%d\n", something, B, s, n, other);
    

        
        return 0;
    }
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • The `scanf_s` conversion stops at the first character it cannot convert, which is typically (but not necessarily) a space or a newline, and that character remains in the input buffer. It will be read by the *next* `scanf_s()`. Format specifiers `%d` and `%s` and `%f` automatically filter such leading whitespace characters, but `%c` and `%[]` and `%n` do not. You can instruct `scanf_s` to do so by adding a space just before the `%`. So please try `scanf_s(" %50[^\n]", str, 50);` with an added space. – Weather Vane Nov 05 '20 at 19:58
  • Thanks so much, this solved the problem – Tomáš Vávra Nov 06 '20 at 06:44
  • The duplicate is not exact, but the same reason applies. – Weather Vane Nov 06 '20 at 09:21

0 Answers0