Identifiers
Time Limit: 1000MS Memory limit: 65536K
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2163
题目描述
An identifier is a sequence of characters. A valid identifier can contain only upper and lower case alphabetic characters, underscore and digits, and must begin with an alphabetic character or an underscore. Given a list of chararcter sequences, write a program to check if they are valid identifiers.
输入
输出
For each of the N lines, output "Yes" (without quote marks) if the character sequence contained in that line make a valid identifier; output "No" (without quote marks) otherwise.
示例输入
7 ValidIdentifier valid identifier valid identifier 0 invalid identifier 1234567 invalid identifier adefhklmruvwxyz12356790 -.,:;!?'"()[]ABCDGIJLMQRSTVWXYZ
示例输出
Yes Yes Yes No No No No
昊哥终于逃了他的形势与政策课程,过来打辅助了。
上场比赛累的一塌糊涂啊。这次终于释放出来了O(∩_∩)O~,最重要不用翻译英语了,好开心~。~
虽然这次并没有做出来很多,但慢慢来,会好起来的!
这道题是第一个做出来的,很水,就是判断输入的字符串是否合法。
简单的来说输入的字符串只能有字母(大写或小写)和下划线,否则都不合法。
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
bool judge_zm(char c)
{
if((c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_') return true;
return false;
}
int main()
{
bool flag;
int i,n,len;
char str[101];
cin>>n;
cin.getline(str,101,'\n');
while(n--)
{
cin.getline(str,101,'\n');
len=strlen(str);
flag=0;
for(i=0;i<len;++i)
{
if(!judge_zm(str[i]))
{
flag=1;
break;
}
}
if(flag) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}