Description
现在又有一个人要加入程序员大家庭,他学习上述操作并输出了一行结果,请你检测他输出的“hello world!”是否正确(没有引号)。对他的要求不应该太高,所以你不用管他每一个字母是大写还是小写,只要字母拼写正确,并且 hello 和 world 中间有一个空格,最后有一个英文的感叹号即可。(感叹号后面不允许再有多余字符)
Input
一行字符,表示他的输入
Output
来检测他的输出是否合法,合法输出Yes
,否则输出 No
。(注意输出时 Yes
和 No
的大小写)
Sample Input 1
HeLLo world!
Sample Output 1
Yes
Sample Input 2
fidg kldf
Sample Output 2
No
Sample Input 3
Helloworld
Sample Output 3
No
Sample Input 4
Hello world!
Sample Output 4
No
Hint
样例1是对的,因为不用在意大小写。样例2的字母拼写有误,样例3中没有空格和感叹号,样例4中空格过多,只能有一个空格。所以 2、3、4样例都是错误的。
没做出来,写的太垃圾了,就不放出来了
与答案不符
判断空格,字母,大小写
如上文output
暴力枚举判断即可:
[C++代码]
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
if (s[0] != 'H' && s[0] != 'h') cout << "No\n";
else if (s[1] != 'E' && s[1] != 'e') cout << "No\n";
else if (s[2] != 'L' && s[2] != 'l') cout << "No\n";
else if (s[3] != 'L' && s[3] != 'l') cout << "No\n";
else if (s[4] != 'O' && s[4] != 'o') cout << "No\n";
else if (s[5] != ' ') cout << "No\n";
else if (s[6] != 'W' && s[6] != 'w') cout << "No\n";
else if (s[7] != 'O' && s[7] != 'o') cout << "No\n";
else if (s[8] != 'R' && s[8] != 'r') cout << "No\n";
else if (s[9] != 'L' && s[9] != 'l') cout << "No\n";
else if (s[10] != 'D' && s[10] != 'd') cout << "No\n";
else if (s[11] != '!') cout << "No\n";
else cout << "Yes\n";
return 0;
}
经本人亲测,程序可以跑通。
如果对你有帮助,还请帮忙点个采纳,谢谢!
下面是我的理解,供参考:
根据题目的意思,只需要判断输入的字符串是否是"hello world!”,空格和感叹号是一定要相同,而字母可以大写可以小写,所以题目可以转换为:可以把输入的字符串转换为小写,然后用strcmp()函数和"hello world!”字符串比较就可以了。
参考链接:
ASCII码对照表-完整ASCII码表-我就查查询
#include <stdio.h>
#include <string.h>
int main(void){
char input[100];
char ch;
int i=0;
//获取一行输入到字符数组input
while((ch=getchar())!='\n'&&i<100){
input[i]=ch;
i++;
}
//printf("input=%s\n",input);
i=0;
//把输入的字符数组里的大写字母转换为小写字母,以便于后面比较字符串
while(input[i]!='\0'){
//http://ascii.wjccx.com/
//把input里的字母字符转换为小写
if(input[i]>='A'&&input[i]<='Z'){
input[i] = input[i]+32;
}
i++;
}
//printf("after change ,input=%s\n",input);
//转换为小写字母的input字符数组的字符串和目标字符串比较,如果相同则输出Yes, 否则输出No
if(strcmp(input,"hello world!")==0){
printf("Yes");
}else{
printf("No");
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
string n;
int main(){
getline(cin,n);
if(n[0]=='H'||n[0]=='h')
if(n[1]=='E'||n[1]=='e')
if(n[2]=='L'||n[2]=='l')
if(n[3]=='L'||n[3]=='l')
if(n[4]=='O'||n[4]=='o')
if(n[5]==' ')
if(n[6]=='W'||n[6]=='w')
if(n[7]=='O'||n[7]=='o')
if(n[8]=='R'||n[8]=='r')
if(n[9]=='L'||n[9]=='l')
if(n[10]=='D'||n[10]=='d')
if(n[11]=='!')
{
if(n.size()>11)
cout<<"No";
else
{
cout<<"Yes";
return 0;
}
}
cout<<"No";
return 0;
}
供参考:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[64], ch;
int i = 0;
while((ch = getchar()) != '\n'){
if(isupper(ch))
ch += 32;
str[i++] = ch;
}
str[i] = '\0';
if(strcmp(str,"hello world!") == 0)
printf("Yes");
else
printf("No");
return 0;
}