main.c 4.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/******************************************************************************
 *                               C PROGRAMMING                                *
 *                    BASIC EXERCISES - EXAMPLE SOLUTIONS                     *
 *                                                                            *
 *      Task_4.14:  Letter Counter                                            *
 *      Author:     Dominik Widhalm                                           *
 *      Email:      dominik.widhalm@technikum-wien.at                         *
 *      Date:       2017-07-20                                                *
 *                                                                            *
 ******************************************************************************/


/***** INCLUDES ***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/***** MACROS *****************************************************************/
// Maximum length of the user string
#define STRING_MAX 80


/***** FUNCTION PROTOTYPES ****************************************************/
int get_letter_count (char letter, char string[STRING_MAX+1]);


/***** LOCAL FUNCTIONS ********************************************************/
/******************************************************************************
 *      get_letter_count                                                      *
 *      @brief Function to count the occurrence of a letter in a string.      *
 *                                                                            *
 *      This function returns the number of occurrences of a given letter in  *
 *      an also supplied string.                                              *
 *                                                                            *
 *      @param letter           Letter to be counted                          *
 *      @param string           Search string                                 *
 *      @return                 Number of occurrences                         *
 ******************************************************************************/
int get_letter_count (char letter, char string[STRING_MAX+1]) {
    /* Temporary count variable */
    int count=0;
    int i=0;
    char temp;
    /* Check if letter is in small caps */
    if ((letter >= 'a') && (letter <= 'z')) {
        /* Make letter big */
        letter = (char)((int)letter - (int)('a'-'A'));
    }
    /* Go through the entire string */
    while ((string[i] != '\0') && (i <= STRING_MAX)) {
        /* Check if current character is a small letter */
        if ((string[i] >= 'a') && (string[i] <= 'z')) {
            /* Store character as big letter */
            temp = (char)((int)string[i] - (int)('a'-'A'));
        } else {
            /* Store character unaltered */
            temp = string[i];
        }
        
        /* Check if current character is the desired letter */
        if (temp == letter) {
            /* Increment count */
            count++;
        }
        
        /* Increment string index */
        i++;
    }
    /* Return the number of occurrences */
    return count;
}

/***** MAIN ROUTINE ***********************************************************/
int main (void) {
    /*** Local Variables ***/
    char letter;
    char string[STRING_MAX+1];
    int occurrence;
    
    /* Ask the user to enter the desired letter */
    printf("Please enter the letter to be counted: ");
    /* Read in the user's input */
84
    scanf(" %c%*c",&letter);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    
    /* Ask the user to input the string */
    printf("Please enter the string (max. %d letters):\n",STRING_MAX);
    /* Read in the user's input */
    fgets(string,STRING_MAX,stdin);
    /* Remove trailing newline */
    strtok(string,"\n");
    
    /* Count the number of letter occurrences */
    occurrence = get_letter_count(letter,string);
    
    /* Output the result */
    printf("\nThe letter '%c' is contained %d times!\n\n",letter,occurrence);
    
    /* Notify the user about the termination of the program */
    printf("The program will now be terminated...\n");
    
    return 0;                           /* Return with Success (0)            */
}
/******************************************************************************/