2024 C语言合法标识符

#include<iostream>
#include<string>
using namespace std;

int main()
{
	int n;
	while (cin >> n)
	{
		string s;
		cin.ignore();//非常关键 忽略输入流的一个字符(默认参数为1)
		while (n--&&getline(cin, s))
		{
			int flag = 1;
			if (!(s[0] == '_' || (s[0] <= 'Z'&&s[0] >= 'A') || (s[0] <= 'z'&&s[0] >= 'a')))
			{
				cout << "no" << endl;
				continue;
			}
			for (auto c : s)
			{
				if (!(c == '_' || (c <= 'Z'&&c >= 'A') || (c <= 'z'&&c >= 'a') || (c >= '0'&&c <= '9')))
				{
					cout << "no" << endl;
					flag = 0;
					break;
				}
			}
			if (flag)
				cout << "yes" << endl;
		}
	}
	return 0;
}
ignore函数:
功能:函数用于输入流。它读入字符,直到已经读了num 个字符(默认为1)或是直到字符delim 被读入(默认为EOF).
其调用形式为cin.ignore(n,终止字符)
原型:istream &ignore( streamsize num=1, int delim=EOF );
函数作用是跳过输入流中n个字符,或在遇到指定的终止字符时提前结束(此时跳过包括终止字符在内的若干字符)。
功能:函数用于输入流。它读入字符,直到已经读了num 个字符(默认为1)或是直到字符delim 被读入(默认为EOF).
其调用形式为cin.ignore(n,终止字符)
原型:istream &ignore( streamsize num=1, int delim=EOF );
函数作用是跳过输入流中n个字符,或在遇到指定的终止字符时提前结束(此时跳过包括终止字符在内的若干字符)。
程序举例:
#include <iostream>
using namespace std;
int main()
{
char array[8];
cin.ignore(6,'a');
cin.getline(array,8);
cout<<array<<endl;
return 0;
}
它还可以这样用:
cin.ignore();
即不带参数;此时默认参数为1.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值