简体   繁体   English

以下语句如何产生空格?

[英]How does following statement produce spaces?

What my program "upper" trying to do is making letters upper case.我的程序“upper”试图做的是使字母大写。 It gets a file from the commmand line as argv;它从命令行获取一个文件作为 argv; then reads it afterwards it makes them uppercase.然后在之后读取它使它们大写。

An example: "i wonder if it works" in the example.txt file.一个例子:example.txt 文件中的“我想知道它是否有效”。 In command line:在命令行中:

C:\Users\...>upper example.txt 
I WONDER IF IT WORKS

This was the code I used first:这是我第一次使用的代码:

int main (int argc, char *argv[]){
    FILE * fp;
    int ch;

    if ((fp = fopen (argv[1] , "r+")) == NULL) {
        fprintf (stderr , "Can not be opened.");
        exit(EXIT_FAILURE);
    } 

    while((ch = getc(fp)) != EOF){
        if (isalpha(ch))
            putchar(toupper(ch));
        else                       
            putchar(' ');
    }

    fclose(fp);
    return 0; 
}

It works but I saw a more concise version which doesn't need the else statement.它有效,但我看到了一个更简洁的版本,它不需要else语句。

while((ch = getc(fp)) != EOF){
    putchar(toupper(ch));
}

And it puts the spaces between each word too.它也在每个单词之间放置了空格。 How is this possible?这怎么可能?

From the documentation文档

int toupper(int c);

Converts c to its uppercase equivalent if c is a lowercase letter and has an uppercase equivalent.转换c到如果大写形式c为小写字母,并有一个大写形式。 If no such conversion is possible, the value returned is c unchanged.如果不可能进行这样的转换,则返回的值c不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM