问题描述
我一直在尝试将字符串数组中的字符串的第一个字母大写我尝试了很多方法,但没有一个是我的代码:
#include <stdio.h>
#include <stdlib.h>
int nw=0;
char words[30][50];
int main(){
printf("enter your words when you finish write b\n");
do{
nw++;
scanf("%s",&words[nw]);
}while(strcmp(&words[nw],"b")!=0);
printf("%s",toupper(&words[1][0]));
}
我该怎么办请帮忙
解决方法
对于初学者,您需要包含标题 <ctype.h>
和 <string.h>
#include <ctype.h>
#include <string.h>
声明这些变量没有多大意义
int nw=0;
char words[30][50];
在文件范围内。它们可以在 main
中声明。
代替这个调用
scanf("%s",&words[nw]);
你至少要写
scanf("%s",words[nw]);
do-while 语句中的条件应该是这样的
while( nw < 30 && strcmp(words[nw],"b") != 0 );
而不是这个电话'
printf("%s",toupper(&words[1][0]));
你应该写
words[1][0] = toupper( ( unsigned char )words[1][0] );
printf( "%s",words[1] );
这是一个演示程序。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char words[30][50] =
{
"hello","world!"
};
for ( size_t i = 0; words[i][0] != '\0'; i++ )
{
words[i][0] = toupper( ( unsigned char )words[i][0] );
printf( "%s ",words[i] );
}
putchar( '\n' );
return 0;
}
程序输出为
Hello World!