Hi folks,

I have an array of chars letters[], it has both mixed letters, and symbols. I nned a code that keeps a count of each letter entered, and any a count for all other non-letter symbols entered.

Thanks in advance and God Bless.
Jonn.

Recommended Answers

All 16 Replies

Post your atempt.

there are only 255 possibilities, so just make an int array 255 and use the character as the index

int count[255] = {0}
char c = 'A';
++count[c];

when done the elemenets of count > 0 is the frequency you want.

Hi All,

The program is supposed to uppercase the characters entered, transpose them (A to B, B to C ... Z to A). And also keep a count of all alphaitalics, how many A's, how many B's .... Z's. Also a count for all other non-alphaitalic characters entered. At the end output which alphaitalic occured the most.

I cannot figure out how the program to count the characters. The upper case transformation works, but does not work for z to Z.

Thanks.
JONN.

#include <iostream>
using namespace std;
const char DECLARED_SIZE = 96;
void uppercase(char letters[]); 
int main()
{
cout << "ENTER UPTO 96 CHARACTERS, FOLLOWED BY A '#':\n";
char letters[DECLARED_SIZE], next;
int index = 0;
int count = 0;
for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
index = 0;
cin >> next;
 
while ((next != '#') && (index < DECLARED_SIZE))
{
letters[index] = next;
index++;
cin >> next;
}
letters[index] = next;
int num_used = index;
cout << "YOU ENTERED: \n";
for (index = 0; index < num_used; index++)
cout << letters[index] << " ";
cout <<endl;
uppercase(letters); 
return 0;
} 
//CHANGE LOWERCASE TO UPPERCASE.
void uppercase(char letters[])
{ 
int size = 0;
int k = 0;
char * answer;
for (k = 0; letters[k] != '#'; k++)
{
size++;
}
answer = new char[size + 1];
for (k = 0; k < size; k++)
{
if ((letters[k] < 122) && (letters[k] > 96))
{
answer[k] = letters[k] - 31;
 
if ((letters[k] < 90) && (letters[k] > 64)) 
{ 
answer[k] = letters[k] + 1;
 
if (letters[k] = 122) 
{
answer[k] = letters[k] - 57;
 
if (letters[k] = 90)
{
answer[k] = letters[k] - 25;
}
}
}
}
 
else 
{
answer[k] = letters[k];
}
cout << answer[k] << " ";
cout <<endl;
 
}
return;
 }

Please use code tags when posting code. Its not all that difficult and makes your post look a whole lot better.

>>for(index = 0; index < DECLARED_SIZE; ++index)letters[index] = 0;
This line is not necessary if you declare the array with an initializer, like this:

char letters[DECLARED_SIZE] = {0}, next;

Isn't function uppercase() supposed to convert the array to all upper case characters? Then why did you make it so difficult? It can be done in only a couple lines of code

void uppercase(char letters[])
{
       int k;
       for (k = 0; letters[k] != '#'; k++)
               letters[k] = toupper(letters[k]);
}

I need a code that keeps a count of each letter entered, and any a count for all other non-letter symbols entered.

The code you posted does not do that.

You really do not need array letters at all, unless required by your instructor. what you need is an array of integers that keeps a count of the number of times a letter is entered. After you enter a character from the keyboard (or a file), convert the character to upper case then increment the array element (see my previous post for example).

Here's the code for a working solution i just made. What you do with the counters at the end is up to you though. Good luck!

#include <iostream>

using namespace std;

int main()
{
    // counter is an int array containing a counter for each possible character
    // entered. counter[0] is used for 'a', counter[1] for 'b', counter[27] for
    // 'A' and so on. counter[52] is used for all other symbols
    int counter[53] = {0};

    // MAX_SIZE is the maximum number of characters to read from the screen
    const int MAX_SIZE = 80;
    // letters is the array containing the inputted string
    char letters[MAX_SIZE];
    cout << "Enter your string: ";
    cin.get(letters,MAX_SIZE);

    // here we will check all characters from the inputted string and allocate the
    // counters in the counter array accordingly
    for (int i=0; i<MAX_SIZE; i++)
    {
        switch (letters[i])
        {
            case 'a': counter[0]++; break;
            case 'b': counter[1]++; break;
            case 'c': counter[2]++; break;
            case 'd': counter[3]++; break;
            case 'e': counter[4]++; break;
            case 'f': counter[5]++; break;
            case 'g': counter[6]++; break;
            case 'h': counter[7]++; break;
            case 'i': counter[8]++; break;
            case 'j': counter[9]++; break;
            case 'k': counter[10]++; break;
            case 'l': counter[11]++; break;
            case 'm': counter[12]++; break;
            case 'n': counter[13]++; break;
            case 'o': counter[14]++; break;
            case 'p': counter[15]++; break;
            case 'q': counter[16]++; break;
            case 'r': counter[17]++; break;
            case 's': counter[18]++; break;
            case 't': counter[19]++; break;
            case 'u': counter[20]++; break;
            case 'v': counter[21]++; break;
            case 'w': counter[22]++; break;
            case 'x': counter[23]++; break;
            case 'y': counter[24]++; break;
            case 'z': counter[25]++; break;
            case 'A': counter[26]++; break;
            case 'B': counter[27]++; break;
            case 'C': counter[28]++; break;
            case 'D': counter[29]++; break;
            case 'E': counter[30]++; break;
            case 'F': counter[31]++; break;
            case 'G': counter[32]++; break;
            case 'H': counter[33]++; break;
            case 'I': counter[34]++; break;
            case 'J': counter[35]++; break;
            case 'K': counter[36]++; break;
            case 'L': counter[37]++; break;
            case 'M': counter[38]++; break;
            case 'N': counter[39]++; break;
            case 'O': counter[40]++; break;
            case 'P': counter[41]++; break;
            case 'Q': counter[42]++; break;
            case 'R': counter[43]++; break;
            case 'S': counter[44]++; break;
            case 'T': counter[45]++; break;
            case 'U': counter[46]++; break;
            case 'V': counter[47]++; break;
            case 'W': counter[48]++; break;
            case 'X': counter[49]++; break;
            case 'Y': counter[50]++; break;
            case 'Z': counter[51]++; break;
            default : counter[52]++; break;
        }
    }
    return 0;
}

may4life -- it may be a somewhat working solution (there is at least one bug I can spot), but much too long and too difficult. It can be greatly simplified by using an array of 255 then using the letter entered as the index into the array. That eleminates that huge switch statement.

Thanks,
You guys are a great help. I was not supposed to use any library functions, that eliminated the uppercase function. Also the array was to have a maximum of 96 characters only.

JONN.

Thanks,
You guys are a great help. I was not supposed to use any library functions, that eliminated the uppercase function. Also the array was to have a maximum of 96 characters only.

JONN.

The array of 96 characters is a limit on the number of characters you should enter from the keyboard. You need a another integer array to count the number of times a character was entered -- for example how may times did you enter the letter 'A', and how many times did you enter '1' ? Your program does not currently tell you that.

If I use this code:

int count[255] = {0}
char c = 'A';
++count[c];

How do I output the count of each character, what code do I use?

something like this perhaps?

char i;
 
for (i = 'a' ; i <= 'Z'; i++)
{
printf("Char: %c was found %d times", i, count[i]);
 
}

I didn't test it, but it should work fine!

Thanks all,

It is true what they say, "ALL WHO SEEK, SHALL FIND".

JONN.

something like this perhaps?

char i;
 
for (i = 'a' ; i <= 'Z'; i++)
{
printf("Char: %c was found %d times", i, count[i]);
 
}

I didn't test it, but it should work fine!

Yes, something like that, but a little different. because it must also count the number of times non-alpha characters were entered

char i;
 
for (i = 0 ; i < 255; i++)
{
    if( count[i] > 0)
         printf("Char: %c was found %d times", count[i], count[i]);
 
}

I will go ahead and try using what I've obtained. I hope my instructor lets me use the uppercase function. I do not have C++ on my work computer, but I will work on this tonight when I get home. I will post what came about tomorrow. "GOD BLESS YOU ALL".

JONN.

it must also count the number of times non-alpha characters were entered

whoops, missed that one, but your right!

commented: A great help, and makes it easy for you. +1

Another way to do toupper without a function (just subtract 32 from the lower-case letter)

if( letters[i] >= 'a' && letters[i] <= 'z')
   letters[i] -= 32;

whoops, missed that one, but your right!

I think your second code will work, I will figure out how to print out the count of all the other characters.

Thanks Again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.