你想读一个字。为此,您需要一个char
具有一些预定义大小的数组。所以改变
char kalimat;
至
char kalimat[64]; /* Can hold 63 chars, +1 for the NUL-terminator */
接下来,您要扫描一个单词。所以改变
scanf("%[^\n]",&kalimat);
至
scanf("%63s", kalimat);
这里所做的更改是
%s
用于扫描单词的用法与用于扫描字符的用法相反%c
。
- 删除与号,因为
%s
需要 a char*
,而不是 a char**
or char(*)[64]
。
- 使用长度说明符(此处为 63)以防止缓冲区溢出。
那么,如果你想
大写数组/单词的第一个字符,使用
kalimat[0] = toupper(kalimat[0]);
或者
*kalimat = toupper(*kalimat);
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]);
但是......你可能需要改变
getchar ();
至
int c; /* Declare at the start of `main` */
while((c = getchar()) != EOF && c != '\n');
为了防止控制台关闭。
固定代码:
#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);
}