CHALLENGE: The variable name is entered from the keyboard. Determine if it is correct from the point of view of C ++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

CHALLENGE: The variable name is entered from the keyboard. Determine if it is correct from the point of view of C ++?

The variable name is entered from the keyboard. Determine if it is correct from the point of view of C ++? The correct name of a variable can not begin with a digit, can not be longer than 256 characters, and does not contain special characters (except for '_') Code in C++

28th Nov 2017, 9:06 PM
Tretyakov Andrey
Tretyakov Andrey - avatar
2 Answers
+ 5
Here's my attempt, this is good, teaches how to write valid variable names :) https://code.sololearn.com/cYCc13sL2Kg1/?ref=app
29th Nov 2017, 7:40 AM
Ipang
+ 3
#include <iostream> #include <regex> using namespace std; int main() { string var; getline(cin,var); const regex pattern ("^[a-z_A-Z]\\w*
quot;); if(regex_match(var,pattern)&&var.length()<256) cout<<"Yes, "<<var<<" is in accordance" <<" with the C++ Rules."; else cout<<"No, "<<var <<" has an incorrect syntax."; }
29th Nov 2017, 5:50 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar