Write a C++ FUNCTION which verifies whether an input string represents a “valid
ID: 3771397 • Letter: W
Question
Write a C++ FUNCTION which verifies whether an input string represents a “valid C++ file name” with file extension cpp. Use the following definition of a “valid C++ file name”: 1) the last four characters of the string must end with “.cpp”, 2) the string must have at least one character before the “.cpp” file extension, and 3) all characters before the file extension must be alphabetic only. For example, “myprog.cpp” is valid. But, “ .cpp”, “.cpp”, “circle.cpx”, “7.cpp”, “a_prog.cpp”, and “aprog.cpp “ are all invalid.
The function takes a single string input parameter and returns a value of type bool, i.e. it returns true only if the input string is a “valid C++ file name” (as defined above). Hint: The ASCII code range for A-Z is 65-90 and a-z is 97-122. Also, you may find the string operations length and substr helpful.
the solution is:
bool validCppFileName(const string filename)
{
bool valid = false;
int length = filename.length();
int extIndex = length – 4;
if ((length > 4) && (filename.substr(extIndex, 4) == “.cpp”))
{
string prefix = filename.substr(0, extIndex);
int i = 0;
valid = true;
while (valid && i < prefix.length())
{
int ascii_code = int(prefix[i]);
if (ascii_code < 65 || ascii_code > 122 || (ascii_code > 90 &&
ascii_code < 97))
{
valid = false;
}
3
i++;
}
}
return valid;
}
could someone please break it down and explain it to me? thanks
Explanation / Answer
//here the function validCppFileName has return value as bool i.e. T/F and takes
//in argument as a string filename i.e name of a file
//constant because you dont want to mess up with the filename
bool validCppFileName(const string filename)
{
//we are assuming that the file name is not valid
bool valid = false;
//filename.length() returns the length of the string
int length = filename.length();
//this means we are removing the .cpp part
//since .cpp has 4 characters hence we subtracted 4 from original length
//to get the filename without extension
int extIndex = length - 4;
//now the length should be >4 because the extension alone takes 4 letters
//filename.substr(extIndex, 4) will give you a substring of string filename
//starting at index "extIndex" AND the substring length will be 4
//this is to get the extension part out
//i.e. (filename.substr(extIndex, 4) simply takes out extension part
if ((length > 4) && (filename.substr(extIndex, 4) == ".cpp"))
{
//prefix is a string which will contain the filename without the extension part
//filename.substr(0, extIndex) gives you a substring starting from index 0
//and substring length should be = extIndex
string prefix = filename.substr(0, extIndex);
//initialization
int i = 0;
//we are assuming the filename is valid
valid = true;
//this loop will break when we are sure that filename is not valid i.e. valid = false
//OR when the iterator i exceeds the length of the substring prefix
//here we are basically iterating over the length of the substring prefix in
//order to check each and every character of substring prefix
while (valid && i < prefix.length())
{
//this is used to get the ascii code of a character
//ascii code of 'A'-'Z' is 65-90 and 'a' - 'z' is 97-122
int ascii_code = int(prefix[i]);
//so if the ascii_code is not within [65,90] OR [97,122] then the character
//is not alphabetic hence the substring prefix is invalid
// when we get to know that substring prefix is invalid then our job is done
// and we can break from the loop
if (ascii_code < 65 || ascii_code > 122 || (ascii_code > 90 &&
ascii_code < 97))
{
valid = false; //means prefix is invalid
}
i++; //going on to the next character in prefix
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.