Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的简单toupper编码_C_Toupper - Fatal编程技术网

C语言中的简单toupper编码

C语言中的简单toupper编码,c,toupper,C,Toupper,我想在我的笔记本电脑(Windows7)上做一个简单的toupper编码。看来我写的任何东西开头都只大写1个字 是否使用%s/%c/%[^\n] 我该怎么办 使用微软Visual C++ 2010 Express < /P> #include <stdio.h> #include <ctype.h> int main() { char kalimat; scanf ("%[^\n]",&kalimat); kalimat=toupper(

我想在我的笔记本电脑(Windows7)上做一个简单的toupper编码。看来我写的任何东西开头都只大写1个字

是否使用%s/%c/%[^\n]

我该怎么办

使用微软Visual C++ 2010 Express < /P>

#include <stdio.h>
#include <ctype.h>

int main()
{
    char kalimat;
    scanf ("%[^\n]",&kalimat);
    kalimat=toupper(kalimat);
    printf("%s",kalimat);
    getchar ();
    return(0);
}
#包括
#包括
int main()
{
查尔·卡利马特;
scanf(“%[^\n]”,&kalimat);
kalimat=toupper(kalimat);
printf(“%s”,kalimat);
getchar();
返回(0);
}

你想读一个单词。为此,需要一个预定义大小的
char
数组。所以改变

char kalimat;
scanf("%[^\n]",&kalimat);

接下来,您要扫描一个单词。所以改变

char kalimat;
scanf("%[^\n]",&kalimat);

这里所做的更改是

  • 用于扫描单词的
    %s
    ,与用于扫描字符的
    %c
    相反
  • 删除与,因为
    %s
    需要
    字符*
    ,而不是
    字符**
    字符(*)[64]
  • 使用长度说明符(这里是63)以防止缓冲区溢出
  • 那么,如果你想

  • 将数组/单词的第一个字符大写,使用

    kalimat[0] = toupper(kalimat[0]);
    

  • 将数组中的所有字符大写,对数组的每个索引使用循环调用
    toupper

    int i, len; /* Declare at the start of `main` */
    
    for(i = 0, len = strlen(string); i < len; i++) /* Note: strlen requires `string.h` */
        kalimat[i] = toupper(kalimat[i]);
    

    为了防止consle关闭


    固定代码:

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h> /* For `strlen` */
    
    int main()
    {
        int i, len, c;
        char kalimat[64];
    
        scanf ("%63s", &kalimat);
    
        /* `*kalimat = toupper(*kalimat);` */
        /* or */
        /* `kalimat[0] = toupper(kalimat[0]);` */
        /* or */
        /* `for(i = 0, len = strlen(string); i < len; i++) 
            kalimat[i] = toupper(kalimat[i]);` */
    
        printf("%s", kalimat);
    
        while((c = getchar()) != EOF && c != '\n');
        return(0);
    }
    
    #包括
    #包括
    #包括/*用于“strlen”*/
    int main()
    {
    int i,len,c;
    char kalimat[64];
    scanf(“%63s”和kalimat);
    /*`*kalimat=toupper(*kalimat);`*/
    /*或*/
    /*`kalimat[0]=toupper(kalimat[0]);`*/
    /*或*/
    /*`for(i=0,len=strlen(字符串);i
    C库函数:

    int toupper(int c);
    
    将小写字母转换为大写字母

    如果要将字符串的所有字母打印为大写:

    int i=0;
    while(str[i])
    {
     putchar (toupper(str[i]));
     i++;
    }
    
    但是,对于大写单个字符,您可以简单地使用:

     putchar (toupper(mychar));
    
    如果存在一个或返回发送给它的相同字符,则函数返回大写

    在C中,可以存储字符串:

     char *string1="Elvis";
     char string2[]="Presley";
     char string3[4]="King";
    

    kalimat
    只是一个字符,不是字符串。您应该将
    kalimat
    定义为数组。或者使用指针和malloc为字符串提供足够的内存
    int i=0;
    while(str[i])
    {
     putchar (toupper(str[i]));
     i++;
    }
    
     putchar (toupper(mychar));
    
     char *string1="Elvis";
     char string2[]="Presley";
     char string3[4]="King";