Hexx
Hexx

Reputation: 19

Letter occurrences in an sentence C++

Okay, so I'm writing the function below that prints how many times a letter occurs within 3 sentences. When I run this with all the letters of the alphabet it gives me a count that is way off. I think the problem is that it continues to index even after it finishes with 1 line of text, it goes all the way up to 80 even if a sentence is less than 80 characters. The thing is I'm kind of lost as to how I can fix the problem.

#include <iostream>
#include "StringProcessing.h"

int main()
{
    char input[3][80];

    std::cout << "Please enter 3 sentences: ";
    for(int i = 0; i < 3; ++i)
        std::cin.getline(&input[i][0], 80, '\n');

    StringProcessing str(input);

    return 0;
}


void StringProcessing::letterOccurrence(char input[3][80])
{
    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    int freq[26] = {0};

    int i = 0;
    while(i < 26)
    {
        for(int row = 0; row < 3; ++row)
        {
            for(int col = 0; col < 80; ++col)
            {
                if(alphabet[i] == input[row][col])
                    freq[i] += 1;
            }
        }
        i++;
    }

    for(int i = 0; i < 26; ++i)
        std::cout << freq[i] << " ";
}

When given: abcdefghi jklmnopqr stuvwxyz (as 3 separate sentences)

I get: 1 2 1 1 1 1 1 1 1 1 1 1 3 10 1 2 1 4 1 2 2 1 14 2 1 1

Upvotes: 0

Views: 66

Answers (4)

Remy Lebeau
Remy Lebeau

Reputation: 597036

Try something more like this instead:

#include <iostream>
#include "StringProcessing.h"

int main()
{
    char input[3][80];

    std::cout << "Please enter 3 sentences: ";
    for(int i = 0; i < 3; ++i)
        std::cin.getline(input[i], 80, '\n');

    StringProcessing str(input);

    return 0;
}

void StringProcessing::letterOccurrence(char input[3][80])
{
    int freq[26] = {0};

    for(int row = 0; row < 3; ++row)
    {
        char *sentence = input[row];

        for(int col = 0; col < 80; ++col)
        {
            char ch = sentence[col];
            if (ch == '\0') break;

            if ((ch >= 'a') && (ch <= 'z'))
                freq[ch - 'a']++;
            else if ((ch >= 'A') && (ch <= 'Z'))
                freq[ch - 'A']++;
        }
    }

    for(int i = 0; i < 26; ++i)
        std::cout << freq[i] << " ";
}

Upvotes: 0

nsivakr
nsivakr

Reputation: 1595

In other words, your string has characters even after the sentence.

char input[3][80];

This essentially means, you are declaring an array of 3 rows with 80 columns but the data is not set to '\0'. Thus, you should not count beyond the length of each sentence.

Upvotes: 0

catnip
catnip

Reputation: 25388

A simple fix would be to replace this:

for(int col = 0; col < 80; ++col)
{
    if(alphabet[i] == input[row][col])
        freq[i] += 1;
}

with this:

int col = 0;
while (input[row][col])
{
    if(alphabet[i] == input[row][col])
        freq[i] += 1;
    ++col;
}

Upvotes: 2

manveti
manveti

Reputation: 1711

getline automatically null-terminates the string it reads in, so you can just break out of your loop if input[row][col] is '\0'.

Upvotes: 1

Related Questions