-2

Task: Write a program that will read a line of text and output the number of occurrences each letter.

#include <iostream>
#include <string>
#include <cstring>
#define N 100
using namespace std;
int main()
{
    char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
    int alphacount[26];
    char lot[N], *p1;
    int txtlen, *p2;

    cout << " Enter a line of text: " << endl;
    cin.getline(lot, 99);

    txtlen = strlen(lot);

    p1 = lot;
    p2 = &txtlen;

    for (int x = 0; x < *p2; x++)
    {
        for (int y = 0; y < 26; y++)
        {
            if (*p1 == alphabet[y])
            {
                alphacount[y]++;
                p1++;
            }
        }
    }
    cout <<;
}

What is the condition needed, and what variable will be used to output the occurrences of letter? For example:

> enter a line of text : mervlala

Output:

a - 2,
e - 1,
l - 2,
m - 1,
r - 1,
v - 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Phleion
  • 1
  • 1
  • 1
    You need to loop over `alphacount` and print non-null values. – Jarod42 Dec 16 '15 at 01:40
  • off topic: Many easier ways to do this. [`isalpha`](http://en.cppreference.com/w/cpp/string/byte/isalpha) eliminates the need for the `for (int y= 0; y<26 ; y++)` loop. [`tolower`](http://en.cppreference.com/w/cpp/string/byte/tolower) will allow you to catch both upper and lower case letters. – user4581301 Dec 16 '15 at 01:41
  • off topic: `p2` is unnecessary. Never use a pointer where you don't have to. – user4581301 Dec 16 '15 at 01:44
  • your calculation loop isn't correct (although it'll be easier to debug this once you have got your output working) – M.M Dec 16 '15 at 02:41

1 Answers1

6

You are programming on c++, you should use the c++ way.

#include <algorithm>

int main ()
{
    std::string textline;
    std::getline (std::cin,textline);

    for(char ch = 'a'; ch <= 'z'; ch++)
    {
        std::size_t n = std::count(textline.begin(), textline.end(), ch);
        if(n > 0)
            std::cout << " - " << n << "," << std::endl;
    }
}
Captain Wise
  • 480
  • 3
  • 13
  • Good answer. Right way to do it, but almost certainly useless to OP. – user4581301 Dec 16 '15 at 01:49
  • 1
    I don't understand, why useless? – Captain Wise Dec 16 '15 at 01:52
  • On second glance, only want to run the loop to 26. No need to count the NULL. – user4581301 Dec 16 '15 at 01:52
  • For an intro to programming question like this you generally don't want to make the TA feel inferior by using `string`s and `algorithm` – user4581301 Dec 16 '15 at 01:53
  • I don't count the NULL, i don't understand what do you talking about when you say no need to count the NULL, you don't like my answer give another one, i try to help. Sometimes i think it's more easy to use string than char tables. It's even more easy to use C++ than C sometimes. And the OP didn't specify what kind of answer he wanted, he want help, i gave him my help. So sorry if you dont like the answer. – Captain Wise Dec 16 '15 at 01:56
  • Tanks, but I just don't understand your comment :-) – Captain Wise Dec 16 '15 at 01:57
  • This is an answer that is mostly correct (you do try to count the NULL on `alphabet` and the loop around count makes this O(M*N) when it could just be N) demonstrates a more C++ way of thinking about the problem, and isn't something that can be copy-pasted and handed in. OP still needs to do their homework. – user4581301 Dec 16 '15 at 02:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98045/discussion-between-captain-wise-and-user4581301). – Captain Wise Dec 16 '15 at 02:09
  • You may even write `for(char c = 'a'; c <= 'z'; ++c)` and get rid of `char alphabet[27]`. – Jarod42 Dec 16 '15 at 02:21
  • @CaptainWise your code counts `alphabet[26]` however that is a null character (the letters are in the indices `0` through `25`) – M.M Dec 16 '15 at 02:42
  • @Jarod42 only works if run on a system where `'a'` through `'z'` are sequential character codes. The original code didn't have this dependency (and what's more, is more easily modified to check a different set of characters). – M.M Dec 16 '15 at 02:43
  • Oh yes i understand about what you are thaking, yes i just copied the OP array and didn't take attention on the size and number of letters, yes english have 26 letters. And yes Jarod42 you are write, using char himself will make even more easy. I modify my post – Captain Wise Dec 16 '15 at 02:52
  • Sorry guys i'm not an english person and at first i did not uderstand user4581301 – Captain Wise Dec 16 '15 at 02:54
  • @M.M: In which system it is not the case ? – Jarod42 Dec 16 '15 at 10:50
  • You should [scope `size_t` into `std`](http://stackoverflow.com/questions/5813700/difference-between-size-t-and-stdsize-t). – Quentin Dec 16 '15 at 14:09