1. Write the function OnlyCapitals that takes a string S and checks if it contains only capital letters. The function returns 1 if the S contains only capital letters, 0 otherwise.
  2. Write the function CountCapitalLetters that takes a string S and an array T (of fixed size 26) and counts in T the number of occurrence of each capital letter found in S. We suppose that we put in the first element of T the number of occurrence of A, in the second element of T the number of occurrence of B, and So on…
    Example: if the string S is "AMBMABDAMCMZM", the array T is:
    \(\begin{array}{c c c c c c c c c c c c c c c c c c c c c c c c c c} A&B&C&D&E&F&G&H&I&J&K&L&M&N&O&P&Q&R&S&T&U&V&W&X&Y&Z \\ 3&2&1&1&0&0&0&0&0&0&0&0&5&0&0&0&0&0&0&0&0&0&0&0&0&1\end{array}\)
  3. Write a main program that:
  • Asks the user to enter two strings S1 and S2 by making sure that each of them contains exclusively capital letters (using the function OnlyCapital).
  • The program should then call the function CountCapitalLetters to get in arrays T1 and T2, the number of occurrence of each capital letter in strings S1 and S2 then compute (in a different array T) the number of occurrence of each letter in strings S1 and S2.
  • Finally the program should use the two arrays T1 and T2, to check if the String S1 is a substring of S2 (all the letters of S1 appears in S2). 

Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define SIZE 50
#define N 26

int OnlyCapitals(char S[])
{
	int i;
	for(i=0;S[i]!='\0';i++) 
		if(S[i]<'A' || S[i]>'Z') return 0;
	return 1;
}

void CountCapitalLetters(char S[], int T[])
{
	int i;
	for(i=0;S[i]!='\0';i++)
		T[S[i]-'A']++;
}

main () 
{
	int i,j;
	char s1[size],s2[size];
	int i,j,T1[N]={0},T2[N]={0},sub;
	do
	{
		printf("Enter s1: ");
		gets(s1);
	} while(OnlyCapitals(s1)==0);

	do
	{
		printf("Enter s2: ");
		gets(s2);
	} while(OnlyCapitals(s2)==0);

	CountCapitalLetters(s1,T1);
	CountCapitalLetters(s2,T2);

	for(i=0;i<N;i++) 
		printf("%d ",T1[i]);
	printf("\n");

	for(i=0;i<N;i++) 
		printf("%d ",T2[i]);
	printf("\n");

	sub=1;
	for(i=0;i<N;i++)
		if(T1[i]>T2[i]) 
		{
			sub=0;
			break;
		}

	if(sub) 
		printf("%s is a substring of %s\n",s1,s2);
	else 
		printf("Not substring\n");

	getch();
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Path with a given sum in binary trees