Sorting and 2d Arrays – Strings – Problem #2
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
You can put multiple strings within a 2D character array where each row of the character array holds one string. For example char arr[3][100] can hold three strings, each of length of maximum 100 characters and the first string is identified by arr[0], the second string by arr[1] and so on. Declare a 2D array string[5][1000] that can hold 5 strings of maximum 1000 characters each and take input the 5 strings. Each string is a sentence containing any number of words. Words consist of alphabets (you may consider all lowercase characters for simplicity) only and no numericals or special characters, and two words are separated only by one blank space. No need to use fullstop also to mark the end of the string. Hence perform the following operations as defined by the following functions. • void characterCount(char *str, int *vowel, int *consonant) - It counts the total number of vowels and number of consonants of a string pointed by char *str, and writes the vowel count to the address pointed by int *vowel and similarly the consonant count to the address pointed by int *consonant. A space is neither a vowel nor a consonant. • int wordCount(char *str) - It returns the number of words of the string pointed by char *str. • void excessWord(char *str1, char*str2) - This function first calls wordCount() to get to know the number of words of each of the two strings char *str1 and char *str2. Hence it prints the excess words that are present in the longer string. For example if the first string contains 4 words and the second string contains 6 words, then it prints words 5 and 6 of the second string. If both the strings contain equal number of words, then it prints “No excess word”. • void str pair compute(char **arr, int number of strings) - This function takes as input the base address of the entire 2D array containing all the strings and another integer denoting the number of strings within the 2D array. Hence it displays the pairs of strings which contain the same words. The words need not be in the same order. In case no two strings contain the same words, then it prints “Not Applicable”. This function may also use wordCount() if required. Consider the example as shown. Suppose the 5 strings in string[][] are, – abcd sdfg qwerty oiuyt – cfr ojhg nsert – evjs irjcl perry – nsert cfr ojhg – qwerty sdfg abcd oiuyt Here, (string[0] and string[4]) and (string[1] and string[3]) contain the same words. So the function displays (1,5) and (2,4) as output, i.e the 1st string and 5th string and 2nd string and 4th string. From main() after taking the 5 string as input, first call characterCount() for each of the 5 strings to print the vowel and consonant count within main() itself. Declare integer variables as required. Hence take input two numbers between 1 and 5 that represent any two strings and call excessWord() that prints the excess words among the two strings. Note that excessWord() in turn calls wordCount(). It is upto you how you wish to display the different outputs, but it should maintain the specifications as given. Finally call str pair compute() to display the pairs of strings which have the same words in different orders.