counting letters in a string.

I am trying to traverse a string. This Program basically counts the amount of letters in a string and returns a number. I can't figure out this program. All I get from the Number of 'a's is just 1.

Example:

Enter a letter: a
Enter text: applesauce
Number of 'a's: 2

#include <iostream>
using namespace std;

char checkCharacter();
int count();

int main()
{
char letter[256];
char text[256];

cout << "Enter a letter: ";
cin >> letter;
cin.getline(text, 256);

cout << "Enter text: ";
cin >> text;

cout << "Number of '" << letter << "'s: " << count;
return 0;
}

char countLetters(char letter[256], char text[256])
{
string str = text;
char checkCharacter;
int count = 0;
int size = 0;

for (int i = 0; i < letter[256]; i++)
{
if (str[i] == checkCharacter)
{
++ count;
}
}

return 0;
}
First, please post code in code tags. See http://www.cplusplus.com/articles/jEywvCM9/


You try to do something, but "cannot figure out the program"?
Have you managed to write a program that you yourself do not know? (As odd it may seem, it is not unrealistic.)
Or perhaps you have chosen a poor program written by somebody else?

Why does the "letter" have 256 letters in it?
Is the "text" read in twice, in two ways?

What is a "count"? The program has at least two different items that are both called with that name.


What operations are done within the function main()?
i < letter[256] should be 256

ideally you would loop
for(int i = 0; letter[i] != 0 && i < maxstringsize; i++) //you need to define maxstringsize = 256
this prevents iterating over letters that are uninitialized and getting a bad count. 0 ends the string, so you stop there.

checkcharacter is uninitialized. this will also not do what you want it to.

return 0 .. wouldnt you want to return the count you got?
return count;

the globals look like function definitions not variables, globals are not good practice, making them look like something else is worse practice, and overriding them with locals of the same name is even worse. Lose the globals and use paramaters.

why is letter a "string" of 256 when you appear to only want 1 character?
why do you read this way:
cin.getline(text, 256); //is this to clear the input buffer?
cout << "Enter text: ";
cin >> text; //discards the getline reading

You do not actually call your functions. You just write out the value of count.
Last edited on
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
#include<iostream>
#include<cstring>
#include<cstdlib>

using namespace std;

size_t countChar(const char *txt, char letter)
{
  size_t len = strlen(txt), count = 0;

  for (size_t i = 0; i < len; i++)
  {
    if (txt[i] == letter)
    {
      count++;
    }
  }
  return count;
}

int main()
{
  char input[256] = {0};
  char letter;

  cout << "Enter a text: ";
  cin.getline(input, 255);
  cout << "Enter a  letter to count: ";
  cin >> letter;

  cout << "There are " << countChar(input, letter) 
       << " " << letter << " in " << input;

  system("pause");
  return 0;
}



Enter a text: applesauce
Enter a  letter to count: a
There are 2 a in applesauce
A really quick and short example of what this might look like after applying the fixes.

const int maxsize = 256;

int countletters(char* in, char val)
{
int count = 0;
int i;
for(i = 0; i < strlen(in); i++)
count += in[i] == val;
return count;
}

int main()
{
char text[maxsize];
char letter;
cin >> text;
cin >> letter; //add your own cout stuff this is just a quick example
cout << countletters(text, letter);
return 0;
}

My two pennyworth:
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
#include <iostream>
using namespace std;

char checkCharacter();  // No matching definition
int count();            // No matching definition
char countLetters(char letter[256], char text[256]); // Missing declaration
// (Note: the previouse prototype is wrong: see below)

int main()
{
    char letter[256]; // "letter" should be a single character
    char text[256];

    cout << "Enter a letter: ";
    cin >> letter;
    cin.ignore(1); // needed to remove the ENTER left in the buffer
    cin.getline(text, 256); // Move this line after "cout"

    cout << "Enter text: "; // Move before getline()
    cin >> text; // If you use getline(), you don't need this any more.

    // STATEMENT MISSING HERE!
    // type count = function which returns the counted occurrences
    cout << "Number of '" << letter << "'s: " << count;
    return 0;
}

// Wrong declaration: letter should be a single character
char countLetters(char letter[256], char text[256])
{
    string str = text;      // You don't need this: use "text"
    char checkCharacter;    // You don't need this: use "letter"
    int count = 0;
    // To discover the size of "text", you can use std::strlen()
    // defined in <cstring>.
    // To do so, add
    // #include <cstring>
    // Then the following line must become:
    // int size = strlen(text);
    int size = 0;

    for (int i = 0; i < letter[256]; i++)   // You bigger problem is here: you
    {                                       // need "size".
        if (str[i] == checkCharacter)       // Use "text" and "letter".
        {
            ++ count;
        }
    }

    return 0; // You nee t return the number of occurrences counted!
}

Topic archived. No new replies allowed.