Nguythang

stringLetter.c

Feb 22nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "string_letter.h"
  6.  
  7. /*
  8.  * Function: GetLetterCount
  9.  * ----------------------------
  10.  *   Count appearance of each character in existing string.
  11.  *   pString: String to be counted.
  12.  *   returns: Array of letter counts.
  13.  */
  14. int* GetLetterCount(char* pString) {
  15.     /* Student code here */
  16.  
  17.         char* string = malloc(sizeof (pString) * sizeof (int));
  18.         int c = 0, count[26] = {0}, *pt;
  19.         for (c = 0; pString[c] != '\0'; c++) {
  20.             if (isupper(pString[c]) != 0)
  21.                 pString[c] = tolower(pString[c]);
  22.         }
  23.         for (c = 0; c < strlen(pString); c++)
  24.               string[c] = pString[c];
  25.         c = 0;
  26.         while (string[c] != '\0') {
  27.             if (string[c] >= 'a' && string[c] <= 'z')
  28.                 count[string[c] - 'a']++;
  29.             c++;
  30.         }
  31.         pt = count;
  32.         for (c = 0; c < 26; c++) {
  33.             if (count[c] != 0)
  34.                 printf("%c : %d\n", c + 'a', count[c]);
  35.         }
  36.         return pt;    
  37. }
  38.  
  39. int main() {
  40.     char input[100];
  41.     printf("Enter a string: ");
  42.     gets(input);
  43.     int* result = GetLetterCount(input);
  44. }
  45.  
  46. stringLetter.h
  47.  
  48. #ifndef STRING_LETTER_H
  49. #define STRING_LETTER_H
  50.  
  51. int* GetLetterCount(char* string);
  52.  
  53. #endif  /* STRING_LETTER_H */
Advertisement
Add Comment
Please, Sign In to add comment