第二届acm省赛C identifier 字符串操作

Identifiers

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

 Identifier is an important concept in the C programming language. Identifiers provide names for several language elements, such as functions, variables, labels, etc.

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.

输入

 The first line of the input contains one integer, N, indicating the number of strings in the input. N lines follow, each of which contains at least one and no more than 100 characters. (only upper and lower case alphabetic characters, digits, underscore (" "), hyphen ("-"), period ("."), comma (","), colon (":"), semicolon (";"), exclamation mark ("!"), question mark ("?"), single and double quotation marks, parentheses, white space and square brackets may appear in the character sequences.)

输出

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

提示

来源

山东省第二届ACM大学生程序设计竞赛

解题思路:

判断是否输入的字符串为合法标识符。


1、字符序列的输入用到  getline(cin,st[i]);

2、在cin>>n 与 getline(cin,st[i]); 之间需要使用 getchar(); 获取输入输入n 之后的回车符。

3、string 与 char [] 类型函数使用不同。  int len =st[i].length();          int len=strlen(st[i]);

4、判断字符c是否为字母和数字的函数(头文件#include <ctype.h>),isalpha(c)  ,   isdigit(c)   是返回非零,不是返回零。



代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

using namespace std;

string st[10005];
int i,j,k;
int n;

int main()
{
    cin>>n;
    getchar();
    for(i=1;i<=n;i++)
    {
        getline(cin,st[i]);
//                    for(j=0;st[i][j]!='\0';j++)
//                    {
//                        cout<<st[i][j];
//                    }
//                    cout<<endl;
        bool b1=1;
        int len=st[i].length();
        //cout<<len<<endl;
        //cout<<st[i][0]<<endl;
        if(!isalpha(st[i][0]) && st[i][0]!='_')
        {
            cout<<"No"<<endl;
            continue;
        }
        for(j=1;j<len;j++)
        {
            if(!isalpha(st[i][j]) && st[i][j]!='_' && !isdigit(st[i][j]))
            {
                b1=0;
                break;
            }
        }
        if(b1)
        {
            cout<<"Yes"<<endl;
        }
        else
            cout<<"No"<<endl;
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值