An identifier in Java, C++ and most other programming languages must
begin with a letter and then may be followed by any number of letters or
digits.
It is possible that underscores (_) will also appear, but only in the middle and never two consecutively.
Write a program to read a string and output whether it is a valid or
invalid identifier. Each string will be 10 characters or less in size.
Example 1:
Enter id: UAB_HSPC
Answer: UAB_HSPC is a valid identifier
Example 2:
Enter id: _a_b__2
Answer: _a_b__2 is not a valid identifier
My ruby solution (which is not complete):
print "Identifier: "
identifier = Array.new
def words(identifier)
identifier.each_char.to_a {|word| words}
end
if words(gets).first.match /^[a-zA-Z]+$/i
print "The identifier is valid."
else
print "The identifier is not valid."
end
No comments:
Post a Comment