When two C-strings have most frequent letter.

I am confused why when I enter for example... sese, I get E as the string (I understand because that appears first) but I get A as the c-string.

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

//Function prototypes
char mostfreq(string);
char mostfreq(char []);

int main()
{
    string words;
    char freq;
    //Generates 100 char space
    char letters[100];

    /*************************************************************************/
    /*******************************STRING************************************/
    cout << "Enter anything you want and I will tell you the most frequent letter." << endl;
    getline(cin, words);

    freq = mostfreq(words);
    cout << "The most frequent letter is " << freq << " in a string." << endl;

    /*************************************************************************/
    /******************************C-STRING***********************************/

    for(int i = 0; i < words.length(); i++)
    {
        letters[i] = words[i];
    }

    freq = mostfreq(letters);
    cout << "The most frequent letter is " << freq << " in a c-string." << endl;
}

char mostfreq(string words)
{
    //Tally mark for each letter that appears
    int wordTally[26];
    int highest = 0;

    //Sets Tally Marks to 0
    for(int i = 0; i < 26; i++)
    {
        wordTally[i] = 0;
    }

    //Changes all words to upper case
    for(int j = 0; j < words.length(); j++)
    {
        if(islower(words[j]))
        {
            words[j] = toupper(words[j]);
        }
    }

    //Subtracts 65(ASCII Decimal for 'A') and uses that number to mark on Tally
    for(int k = 0; k < words.length(); k++)
    {
        words[k] = words[k] - 65;
        wordTally[words[k]]++;
    }

    //Determines which Tally haves the highest number and will be recorded
    for(int l = 1; l < 26; l++)
    {
        if(wordTally[highest] < wordTally[l])
        {
            highest = l;
        }
    }

    //The Tally with the highest count will be returned
    //Add 65 because it was subtracted earlier
    return highest + 65;
}

char mostfreq(char letters[])
{
    int wordTally[26];
    int highest = 0;
    int counter = 0;
    //Vector is used to hold all the letters from the given word
    std::vector <char> wordCheck;

    for(int i = 0; i < 26; i++)
    {
        wordTally[i] = 0;
    }

    //Use vector to avoid all non alphabet characters
    while(letters[counter] != '\0')
    {
        if(isalpha(letters[counter]))
        {
            wordCheck.push_back(letters[counter]);
        }
        counter++;
    }

    //Change all lower cases to upper case
    for(int j = 0; j < wordCheck.size(); j++)
    {
        if(islower(wordCheck[j]))
        {
            wordCheck[j] = toupper(wordCheck[j]);
        }
    }

    //Subtracts 65 to determine where to mark on tally
    for(int k = 0; k < wordCheck.size(); k++)
    {
        wordCheck[k] = wordCheck[k] - 65;
        wordTally[wordCheck[k]]++;
    }

    //Searches for the highest mark on Tally
    for(int l = 1; l < wordCheck.size(); l++)
    {
        if(wordTally[highest] < wordTally[l])
        {
            highest = l;
        }
    }

    //Returns the most frequent letter
    return highest + 65;
}



Here is the result...
1
2
3
4
Enter anything you want and I will tell you the most frequent letter.
sese
The most frequent letter is E in a string.
The most frequent letter is A in a c-string.

Last edited on
Lines 30-33: You never store a \0 terminator into the C-string. mostfreq(char[]) is indexing past the end of the entered string.
You don't loop through all 26 letters in the last loop.
Last edited on
how do i store the \0?
ah... I just realized I made an error on that. Thanks for pointing that out Peter! it works now
does
1
2
3
4
5
6
7
    for(int i = 0; i < words.length(); i++)
    {
        if(i != words.length())
            letters[i] = words[i];
        else if(i == words.length())
            letters[i+1] = '\0';
    }

include the \0 ?
does <snip> include the \0 ?

No. words.length() does not include the trailing \0.

1
2
3
    for(int i = 0; i < words.length(); i++)
        letters[i] = words[i];
    letters[words.length()] = '\0';





makes sense, thanks a lot!
Topic archived. No new replies allowed.