C语言中从键盘输入一个英文句子,输出所有首字母大写的单词.
答案:4 悬赏:20 手机版
解决时间 2021-02-25 18:49
- 提问者网友:焚苦与心
- 2021-02-25 08:07
C语言中从键盘输入一个英文句子,输出所有首字母大写的单词.
最佳答案
- 五星知识达人网友:狂恋
- 2021-02-25 08:46
应用sscanf函数的选择性读取功能和当前读取字符计数功能可方便地解决问题。举例如下:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
int main(void){
char sent[200],tmp[21];
int i=0,x;
printf("Please enter an English sentence...sent:
");
gets(sent);
while(!((x=sent[i]|0x20)>='a' && x<='z'))//跳过前导非字母
i++;
for(;sent[i];i++){
sscanf(sent+i,"%[A-Za-z]%*[^A-Za-z]%n",tmp,&x);//选择性读取并计数
if(*tmp>='A' && *tmp<='Z')//只输出首字母大写的单词
printf("%s
",tmp);
i+=x-1;//根据刚刚读取的单词长度调整下标
}
printf("
");
return 0;
}
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
int main(void){
char sent[200],tmp[21];
int i=0,x;
printf("Please enter an English sentence...sent:
");
gets(sent);
while(!((x=sent[i]|0x20)>='a' && x<='z'))//跳过前导非字母
i++;
for(;sent[i];i++){
sscanf(sent+i,"%[A-Za-z]%*[^A-Za-z]%n",tmp,&x);//选择性读取并计数
if(*tmp>='A' && *tmp<='Z')//只输出首字母大写的单词
printf("%s
",tmp);
i+=x-1;//根据刚刚读取的单词长度调整下标
}
printf("
");
return 0;
}
全部回答
- 1楼网友:平生事
- 2021-02-25 12:50
SMK你个逗比追问你是谁?追答你爸爸
- 2楼网友:傲气稳了全场
- 2021-02-25 11:14
#include
int main(void)
{
char str[256];
char *cp;
printf("Input sentence:");
gets(str);
for(cp = str; *cp != ''; cp++)
{
if((cp == str || *(cp -1) == ' ') && *cp >= 'A' && *cp <= 'Z')
{
while((*cp >= 'A' && *cp <= 'Z') || (*cp >= 'a' && *cp <= 'z'))
{
printf("%c", *cp);
cp++;
}
printf(" ");
}
}
return 0;
}
int main(void)
{
char str[256];
char *cp;
printf("Input sentence:");
gets(str);
for(cp = str; *cp != ''; cp++)
{
if((cp == str || *(cp -1) == ' ') && *cp >= 'A' && *cp <= 'Z')
{
while((*cp >= 'A' && *cp <= 'Z') || (*cp >= 'a' && *cp <= 'z'))
{
printf("%c", *cp);
cp++;
}
printf(" ");
}
}
return 0;
}
- 3楼网友:往事隔山水
- 2021-02-25 10:23
我写了个完整程序给你参考。
源代码如下(vc++6.0下编译通过):
#include
#include
#define MAX_SENTENCE_STR_LEN 1024
int main()
{
char English_sentence[MAX_SENTENCE_STR_LEN] = {0};
char word[32] = {0};
char *token = NULL;
char *seps = " ";
printf("please input a English sentence: \n");
gets(English_sentence);
token = strtok(English_sentence, seps);
while (token != NULL)
{
strcpy(word, token);
if (word[0] >= 'A' && word[0] <= 'Z')
{
printf("%s\n", word);
}
token = strtok(NULL, seps);
}
return 0;
}
希望对你有帮助。
源代码如下(vc++6.0下编译通过):
#include
#include
#define MAX_SENTENCE_STR_LEN 1024
int main()
{
char English_sentence[MAX_SENTENCE_STR_LEN] = {0};
char word[32] = {0};
char *token = NULL;
char *seps = " ";
printf("please input a English sentence: \n");
gets(English_sentence);
token = strtok(English_sentence, seps);
while (token != NULL)
{
strcpy(word, token);
if (word[0] >= 'A' && word[0] <= 'Z')
{
printf("%s\n", word);
}
token = strtok(NULL, seps);
}
return 0;
}
希望对你有帮助。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯