C language keyboard input string input string from lower case to upper case output and scanf small problem solving

1. When the blogger was learning C language, he did not pay much attention to some details of C language. As a result, when someone asked questions later, he did not answer for a while, which is the so-called weak foundation. For example, this time a classmate I asked about a small problem with scanf keyboard input. After struggling for a while, I still figured out the problem.

2. Without further ado, what is to be achieved is to input a string of lowercase letters on the keyboard and then convert it into uppercase for output. The code is as follows:

#include <stdio.h>

int main()
{
    char str[10];
    int i,n;
    printf("输入长度n:");
    scanf("%d",&n);
    printf("输入字符串长度%d:",n);
    for ( i = 0; i < n; i++)
    {
        scanf("%c",&str[i]);
        if (str[i]>='a' && str[i]<='z')
        {
            str[i] = str[i]-32;
        }
        
    }
    for ( i = 0; i < n; i++)
    {
        printf("%c",str[i]);
    }
    return 0;
    
}

Then I input and output and the results are as follows:

The question is, why only outputs AS? Many children may be confused

But smart friends can easily discover it, huh? A blank line is output between this AS and the input string. That is, the red box in the figure below is a blank line. Why is this?

For this fantasy thing, we have to start with the code content.

In the code, we firstenter the length n, and then enter the string, right? Yes, the problem arises here.

When we enter the length n, then press Enter, right? That is to say, we input twice twice (n is entered once, and the Enter key counts once) (the carriage return is also a space), but you may have overlooked it< /span>, how to solve it? Output a blank line (actually output a carriage return), and then output AS, so the above result appears when outputting, str[0] is '\n', and the value of str[1] is ' a', str[2] value is 's', and the following scanf is received as %c, so when we enter n in the previous scanf and then press Enter, the carriage return is The str[0] of the first subsequent for loop is received, so the value of Carriage Enter (\n) belongs to the character and an input of , of course we all know that scanf ends with a space of

3. Two solutions

(1) Add a scanf to receive carriage return, the code is as follows, I added a line scanf("%c",& m);, used to receive the carriage return after inputting n

#include <stdio.h>

int main()
{
    char str[10],m;
    int i,n;
    printf("输入长度n:");
    scanf("%d",&n);
    printf("输入字符串长度%d:",n);
    scanf("%c",&m);
    for ( i = 0; i < n; i++)
    {
        scanf("%c",&str[i]);
        if (str[i]>='a' && str[i]<='z')
        {
            str[i] = str[i]-32;
        }
        
    }
    for ( i = 0; i < n; i++)
    {
        printf("%c",str[i]);
    }
    return 0;
    
}

(2) Add a space before %c, the code is as follows:

#include <stdio.h>

int main()
{
    char str[10];
    int i,n;
    printf("输入长度n:");
    scanf("%d",&n);
    printf("输入字符串长度%d:",n);
    for ( i = 0; i < n; i++)
    {
        scanf(" %c",&str[i]);
        if (str[i]>='a' && str[i]<='z')
        {
            str[i] = str[i]-32;
        }
        
    }
    for ( i = 0; i < n; i++)
    {
        printf("%c",str[i]);
    }
    return 0;
    
}

Why is there a space before %c in scanf(" %c",&str[i]);? Because in scanfFor example, scanf("%c % c %c",&n), in this case, the input will not end with a space, but a space will be used as the input separator for these three.

Therefore, adding a space in front of %c means entering n characters from n to n characters. Separate the input with spaces (carriage returns are also spaces)< a i=2>, so %c will not read carriage return, then str[0] will not be '\n', the result is correct at this time, as follows:

The output also no longer leaves a blank line.

Guess you like

Origin blog.csdn.net/m0_59799878/article/details/134651253