Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "string_letter.h"
- /*
- * Function: GetLetterCount
- * ----------------------------
- * Count appearance of each character in existing string.
- * pString: String to be counted.
- * returns: Array of letter counts.
- */
- int* GetLetterCount(char* pString) {
- /* Student code here */
- char* string = malloc(sizeof (pString) * sizeof (int));
- int c = 0, count[26] = {0}, *pt;
- for (c = 0; pString[c] != '\0'; c++) {
- if (isupper(pString[c]) != 0)
- pString[c] = tolower(pString[c]);
- }
- for (c = 0; c < strlen(pString); c++)
- string[c] = pString[c];
- c = 0;
- while (string[c] != '\0') {
- if (string[c] >= 'a' && string[c] <= 'z')
- count[string[c] - 'a']++;
- c++;
- }
- pt = count;
- for (c = 0; c < 26; c++) {
- if (count[c] != 0)
- printf("%c : %d\n", c + 'a', count[c]);
- }
- return pt;
- }
- int main() {
- char input[100];
- printf("Enter a string: ");
- gets(input);
- int* result = GetLetterCount(input);
- }
- stringLetter.h
- #ifndef STRING_LETTER_H
- #define STRING_LETTER_H
- int* GetLetterCount(char* string);
- #endif /* STRING_LETTER_H */
Advertisement
Add Comment
Please, Sign In to add comment