fork(1) download
  1. #include<stdio.h>
  2. //#include<conio.h>
  3. #include<ctype.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define BOOLEAN int
  7.  
  8. typedef struct WordStructure {
  9. char word[50];
  10. int frequency;
  11. int length;
  12. } Word;
  13.  
  14. BOOLEAN isEqualsIgnoreCase(char original[], char toBeCompared[]);
  15.  
  16. int calcNumOfCharacters(char original[]);
  17.  
  18. int fetchWords(char original[], Word w[]);
  19.  
  20. int isExists(Word w[], Word temp, int length);
  21.  
  22. void getInputText(char original[]);
  23.  
  24. int countWords(Word w[], int length);
  25.  
  26. int countPunctuations(char original[]);
  27.  
  28. int countPrepositions(Word w[], Word preposition[], int wordLength, int prepoLength);
  29.  
  30. double arithmeticMean(Word words[], int length);
  31.  
  32. int noOfWordsWithOddLengths(Word words[], int length);
  33.  
  34. int noOfWordsWithEvenLengths(Word words[], int length);
  35.  
  36. void printALlTheWords(Word words[], int length);
  37.  
  38. void main() {
  39. char original[1000];
  40. char prepositions[1000];
  41. Word words[400];
  42. Word prepo[400];
  43. int wordLength = 0, prepoLength = 0;
  44.  
  45. printf("\nEnter original input text: \n");
  46. getInputText(original);
  47. printf("\nEnter prepositions: \n");
  48. getInputText(prepositions);
  49.  
  50. wordLength = fetchWords(original, words);
  51. prepoLength = fetchWords(prepositions, prepo);
  52.  
  53. printALlTheWords(words, wordLength);
  54. printALlTheWords(prepo, prepoLength);
  55.  
  56.  
  57. printf("\nNumber of characters are: \n%d\n", calcNumOfCharacters(original));
  58. printf("\nNumber of words are: \n%d\n", countWords(words, wordLength));
  59. printf("\nArithmetic Mean: \n%.2f\n", arithmeticMean(words, wordLength));
  60. printf("\nNumber of punctuation are: \n%d\n", countPunctuations(original));
  61. printf("\nNumber of prepositions are: \n%d\n", countPrepositions(words, prepo, wordLength, prepoLength));
  62. printf("\nNumber of words with even length are: \n%d\n", noOfWordsWithEvenLengths(words, wordLength));
  63. printf("\nNumber of words with odd length are: \n%d\n", noOfWordsWithOddLengths(words, wordLength));
  64. }
  65.  
  66. void printALlTheWords(Word w[], int length) {
  67. int i = 0;
  68. printf("\n");
  69. for (i = 0; i < length; i++) {
  70. printf("%20s\t%2d\t%2d\n", w[i].word, w[i].length, w[i].frequency);
  71. }
  72. printf("\n");
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. void getInputText(char original[]) {
  82. int i = 0;
  83. char ch;
  84. while ((ch=getchar()) != '$') {
  85. original[i] = ch;
  86. i++;
  87. }
  88. original[i] = '\0';
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. BOOLEAN isEqualsIgnoreCase(char original[], char toBeCompared[])
  104. {
  105. return TRUE;
  106. }
  107. int calcNumOfCharacters(char original[])
  108. {
  109. int i=0;
  110. while(original[i]!='\0')
  111. i++;
  112. return i;
  113. }
  114. int fetchWords(char original[], Word w[])
  115. {
  116. int end=0,pos,temp=0,i=0,j,l;
  117. Word theWord;
  118. pos = 0;
  119. while(!end)
  120. {
  121.  
  122. while ((!isalnum(original[pos]))&& (original[pos] != '\0'))
  123. pos++;
  124. if(original[pos] != '\0'){
  125. if(i==0)
  126. {
  127. j=0;
  128. while ((isalnum(original[pos])) && (original[pos] != '\0'))
  129. {
  130. w[i].word[j] = original[pos];
  131. j++;
  132. pos++;
  133. }
  134. w[i].length=j;
  135. w[i].frequency=1;
  136. i++;
  137. }
  138. else
  139. {
  140. j=0;
  141. while ((isalnum(original[pos])) && (original[pos] !='\0'))
  142. {
  143. theWord.word[j] = original[pos];
  144. j++;
  145. pos++;
  146. }
  147. theWord.length=j;
  148. theWord.frequency=1;
  149. temp=isExists(w,theWord,i);
  150. if(temp==-1)
  151. {
  152. for(l=0;l<theWord.length;l++)
  153. w[i].word[l]=theWord.word[l];
  154. w[i].length=l;
  155. w[i].frequency=1;
  156. i++;
  157. }
  158. else
  159. w[temp].frequency=w[temp].frequency+1;
  160. }
  161. }
  162. if(original[pos]=='\0')
  163. end=1;
  164.  
  165. }
  166. return i;
  167. }
  168. int isExists(Word SourceArray[], Word toBeFound, int SourceArrayLength)
  169. {
  170. int i,j=-1,k,kontinue=0;
  171. for(i=0;i<SourceArrayLength;i++)
  172. {
  173.  
  174. if(SourceArray[i].length==toBeFound.length)
  175. {
  176. k=0,kontinue=1;
  177. while (k < SourceArray[i].length&&kontinue)
  178. {
  179. if (SourceArray[i].word[k]==toBeFound.word[k]||toupper(SourceArray[i].word[k]) == toupper(toBeFound.word[k]))
  180. {
  181. k++;
  182.  
  183. }
  184.  
  185. else
  186. {
  187. kontinue = 0;
  188. }
  189. }
  190.  
  191. }
  192. if(kontinue)
  193. {
  194. j=i;
  195. break;
  196. }
  197. }
  198. return j;
  199. }
  200. int countWords(Word w[], int length)
  201. {
  202. int sum=0,i;
  203. for(i=0;i<length;i++)
  204. sum=sum+w[i].frequency;
  205. return sum;
  206. }
  207. int countPunctuations(char original[])
  208. {
  209. int i=0,count=0;
  210. while(original[i]!='\0')
  211. {
  212. if(original[i]=='.'||original[i]=='?'||original[i]=='!'||original[i]==':'||original[i]==';'||original[i]=='-'||original[i]=='_'||original[i]==','||original[i]=='\''||original[i]=='"'||original[i]=='/')
  213. count++;
  214. i++;
  215. }
  216. return count;
  217. }
  218. int countPrepositions(Word w[], Word preposition[], int wordLength, int prepoLength)
  219. {
  220. int i,count=0,temp=-1;
  221. for(i=0;i<prepoLength;i++)
  222. {
  223. temp=isExists(w,preposition[i],wordLength);
  224. if(temp!=-1)
  225. count=count+w[temp].frequency;
  226. }
  227. return count;
  228. }
  229. double arithmeticMean(Word words[], int length)
  230. {
  231. int i;
  232. double x,n=0.0,sum=0.0;
  233. for(i=0;i<length;i++)
  234. {
  235. sum=sum+(words[i].length*words[i].frequency);
  236. n=n+words[i].frequency;
  237. }
  238. x=sum/n;
  239. return x;
  240. }
  241. int noOfWordsWithOddLengths(Word words[], int length)
  242. {
  243. int i,count=0;
  244. for(i=0;i<length;i++)
  245. {
  246. if(words[i].length%2!=0)
  247. count=count+words[i].frequency;
  248. }
  249. return count;
  250. }
  251. int noOfWordsWithEvenLengths(Word words[], int length)
  252. {
  253. int i,count=0;
  254. for(i=0;i<length;i++)
  255. {
  256. if(words[i].length%2==0)
  257. count=count+words[i].frequency;
  258. }
  259. return count;
  260. }
Runtime error #stdin #stdout 0s 1796KB
stdin
There are about one hundred and fifty fift prepositions in English. Yet this is ar very small number when you think of the thousands of other words. Prepositions are important words.$around as at but by behind below beneath in inside into of off on up via with for from$
stdout
Enter original input text: 

Enter prepositions: 

               There	 5	 1
                 are	 3	 2
               about	 5	 1
                 one	 3	 1
             hundred	 7	 1
                 and	 3	 1
               fifty	 5	 1
                fift	 4	 1
        prepositions	12	 2
                  in	 2	 1
             English	 7	 1
                 Yet	 3	 1
                this	 4	 1
                  is	 2	 1
                  ar	 2	 1
                very	 4	 1
               small	 5	 1
              number	 6	 1
                when	 4	 1
                 you	 3	 1
               think	 5	 1
                  of	 2	 2
                 the	 3	 1
           thousands	 9	 1
               other	 5	 1
               words	 5	 2
           important	 9	 1


              around	 6	 1
                  as	 2	 1
                  at	 2	 1
                 but	 3	 1
                  by	 2	 1
              behind	 6	 1
               below	 5	 1
             beneath	 7	 1
                  in	 2	 1
              inside	 6	 1
                into	 4	 1
                  of	 2	 1
                 off	 3	 1
                  on	 2	 1
                  up	 2	 1
                 via	 3	 1
                with	 4	 1
                 for	 3	 1
                from	 4	 1


Number of characters are: 
182

Number of words are: 
31

Arithmetic Mean: 
4.81

Number of punctuation are: 
3

Number of prepositions are: 
3

Number of words with even length are: 
12

Number of words with odd length are: 
19