代码和分析
首先需要使用strtok把这个字符串以空格分割成一个一个的单词,然后把每一个单词字符串存进一个数组a里面。
注意2点:
1.不能用scanf传递该句子,因为有空格,要用gets
2.传参的时候,老是报错,因为传的一维进去,函数里面用2维就不会知道下一个字符串的首地址,网上也搜了别人的博客看怎么解决的,如下:
二维数组函数传参博客链接
本题代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 50
void toUpper(char **a,int k);
int main()
{
int k=0;int i;
char ch[2]=" ";
char *token,*a[N];
char str[N];
printf("请输入一条英文句子!\n");
gets(str);//不能用scanf遇到空格结束
//str[N]="I am very happy to meet you!";//或者写死
//分割单词
token=strtok(str,ch);
a[k++]=token;
while(token){
//printf("%s",token);
token=strtok(NULL,ch);
a[k++]=token;
}
k=k-1;//实际长度
// char *p=&a;
toUpper(a,k);
}
void toUpper(char **a,int k){//传一维数组进来但想使用二维的,使用指针的指针,传参用&a 或者a均指数组a的首地址
int i;
for(i=0;i<k;i++){
if(a[i][0]>='a'&&a[i][0]<='z'){//小写字母
a[i][0]=toupper(a[i][0]); //转大写-32,或者用函数toupper, a[i][0]=toUpper(a[i][0]);
}
}
for(i=0;i<k;i++){
printf("%s ",a[i]);
}
}