#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
char str[100] = "hello world i am a small mac computer";
int wordCount=0;
for (long i=0; i<strlen(str); i++) {
// 判断str[0]是不是' ',不是,而且字母是小写
if (str[0] != ' ' && str[0]>='a' && str[0]<='z') {
wordCount++;
str[0] = str[0]-32;
}
// 如果str[i]不是空格而前一个字符是' ',单词数+1, 是小写字母,转换为大写
if (str[i]!=' ' && str[i-1]==' ' && str[i]<='z' && str[i]>='a') {
wordCount++;
str[i] = str[i]-32;
}
}
printf("%s\n",str);
printf("一共%d个单词\n",wordCount);
}
return 0;
}
|
|