Skip to content

Instantly share code, notes, and snippets.

@alastairparagas
Created November 21, 2018 06:08
Show Gist options
  • Select an option

  • Save alastairparagas/1b5efd945a955812e542a26f20f73bce to your computer and use it in GitHub Desktop.

Select an option

Save alastairparagas/1b5efd945a955812e542a26f20f73bce to your computer and use it in GitHub Desktop.
// return 1 if the file name is illegal; otherwise, return 0; legal
// characters for a file name include letters (case sensitive),
// numbers, dots, dashes, and underscores; and a legal file name
// should not be more than MAX_NAME-1 in length
static int illegal_filename(char* name)
{
/* YOUR CODE */
return 1;
}
@cesiabulnes
Copy link

static int illegal_filename(char* name) {
// Iterate over each character of the input file name
for(int i = 0; i< strlen(name); i++) {
if((name[i] >= 'a' && name[i]<= 'z') || (name[i] >= 'A' && name[i] <= 'Z')){
continue;
//if the char is in the alphabet, whether uppercase or lowercase
//continue to the next iteration
}
else if(name[i] >= '0' && name[i] <= '9'){
continue; //if the char is a number, continue to the next iteration
}
else if ((name[i] =='.' ) || (name[i] =='-' ) || (name[i] == '' )) {
continue; //if the char is a .,
,- , continue to the next iteration
}
else {
return 1;
//if the char isn't in the three cases above, then the filename is illegal
}
}

//name of file greater than max_name-1 means it is illegal
if (strlen(name) > MAX_NAME-1){
	return 1;
}

return 0;

}

@cesiabulnes
Copy link

static int illegal_filename(char* name) {
	// Iterate over each character of the input file name
	for(int i = 0; i< strlen(name); i++) {
		if((name[i] >= 'a' && name[i]<= 'z') || (name[i] >= 'A' && name[i] <= 'Z')){
			continue;
			//if the char is in the alphabet, whether uppercase or lowercase
			//continue to the next iteration
		}
		else if(name[i] >= '0' && name[i] <= '9'){
			continue; //if the char is a number, continue to the next iteration
		}
		else if ((name[i] =='.' ) || (name[i] =='-' ) || (name[i] == '_' )) {
			continue; //if the char is a .,_,- , continue to the next iteration
		}
		else {
			return 1;
			 //if the char isn't in the three cases above, then the filename is illegal
		}
	}

	//name of file greater than max_name-1 means it is illegal
	if (strlen(name) > MAX_NAME-1){
		return 1;
	}

	return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment