In this exercise we consider strings denoting complete file names. A complete file name consists of 2 parts: a name and an extension (example: prog.c is a file name, where “prog” is the name, and “c” is the extension).

Therefore, a file name is valid if it satisfies the following conditions:

  • It starts with an alphabetical character
  • It contains one and only one ‘.’ (dot character), that comes between the name and the extension (it does not appear at the beginning or at the end of the file name).

Write the function validName that takes a string and tests whether it is a valid name or not.

Write the function split that takes a file name as string (supposed to be valid) and generates the name and the extensions in two other strings.

Write a C void function extensionType that takes an extension and prints on the screen the file type according to the following listed extension.

Extension

Type of the file

“c”

A C program file

“docx”

A document file

“avi”

A video file

Otherwise

Unknown tyoe

 PS: you may use the function strcmp(char []s1, char []s2) of the string.h library. This function returns 0 if the two strings s1 and s2 are the same.

Write a main function that reads a string (of Max 30 characters) then invokes the validName function. In the case of an invalid name it prints “Invalid” otherwise it prints the name, the extension and the type of the file.  

Output examples:

Execution1

Execution2

Execution3

Enter file name?
1File1.doc
Invalid

Enter filename?
File1.docx
Name: file1
Extension: doc
The file is a document file.

Enter string?
Prog.c
Name: Prog
Extension: c
The file is a C program file.


Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#include<math.h>

int validName(char n[]) {
	int i, c;
	// check if the string begins with a letter 
	if (!((n[0] >= 'a' && n[0] <= 'z') || (n[0] >= 'A' && n[0] <= 'Z')))
		return 0;


	// check if the string contains one and only one dot 
	c = 0;
	for (i = 0; i<strlen(n); i++) {

		if (n[i] == '.')
			c++;
	}

	if (c != 1)
		return 0;

	// check if the dot comes between the name and the ext
	if (n[0] == '.' || n[strlen(n) - 1] == '.')
		return 0;

	return 1;
}

void split(char f[], char n[], char e[])
{
	int i, j, k;
	i = j = k = 0;

	while (f[i] != '.')
		n[j++] = f[i++];
	n[j] = '\0';

	i++; // jump one char 

	while (f[i] != '\0')
		e[k++] = f[i++];
	e[k] = '\0';
}



void extensionType(char e[])
{
	if (strcmp(e, "c") == 0)
		printf("The file is a C program\n");
	else if (strcmp(e, "docx") == 0)
		printf("The file is a document file\n");
	else if (strcmp(e, "avi") == 0)
		printf("The file is a video file\n");
	else printf("Unkown type\n");
}

void main() {
	char fn[30], n[30], e[30];
	puts("Enter filename?");
	gets(fn);

	if (validName(fn) == 1) {
		split(fn, n, e);
		printf("Name: "); puts(n);
		printf("Extension: "); puts(e);
		extensionType(e);
	}
	else
		printf("invalid\n");
	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Width of binary trees - recursive version