0

I am working on a program that has the user input a letter then a string. Once the string is inputted the program should traverse the string and return the amount of the specific letter within the string. Here is the code I have so far:

#include <iostream>
using namespace std;

void countLetters(char letter[]);

/**********************************************************************
* Prompts the user for a line of input (using getline), 
* calls countLetters(), and displays the number of letters.
 ***********************************************************************/
int main()
{
   char letter[256];
   countLetters(letter);
   return 0;
}

/**********************************************************************
* Function to return the number of letters in a string.
 ***********************************************************************/
void countLetters(char Letter[])
{
   char text[] = " ";
   int count = 0;

   cout << "Enter a letter: ";
   cin >> letter;

   cout << "Enter text: ";
   cin >> text;
   cin.getline(text, 256);
   for (int i = (sizeof(text) / sizeof(text[0]) - 2); i >=0; i--)
   {
      if(text[i])
      {
         count++;
      }
   cout << "Number of '" << letter << "'s: " << count << endl;
   }
}

/* 
The output should be:
Enter a number: e
Enter a string: Hello, programming is fun
Number of 'e's: 1
*/

I have tried researching this and have found no help through this method of counting the amount of letters within the string the user inputs. Any help is appreciated, thank you.

  • `if (text[i] == letter)....` – 001 Mar 10 '18 at 01:09
  • `char text[] = " "; cin >> text;` will be undefined behavior for any string entered longer than a single character. Be careful with c-strings. If you're in C++ you'll find everything is much easier with `std::string` – scohe001 Mar 10 '18 at 01:10
  • You are using `sizeof` wrong. This operator returns the size of whatever type is provided in the arguments, not the size of arrays and C-style strings. `std::string`s are much easier to use than C-style strings, and it should be your default when you need to store a string, Consider learning from a [good book](https://stackoverflow.com/q/388242/9254539). – eesiraed Mar 10 '18 at 05:33
  • @FeiXiang With arrays, `sizeof` will return the size of the entire array in bytes. So OP is using it correctly here. – 001 Mar 10 '18 at 15:55
  • @JohnnyMopp You're right, but it shouldn't be used to find the length of C-style strings anyway since that's not equal to the size of the array, which is constant. I should have said the operator does not return the *length* of C-style strings. – eesiraed Mar 10 '18 at 18:45

1 Answers1

0

Most issues have been pointed out in the comments. Here's a fixed version:

#include <iostream>
using namespace std;

void countLetters();

/**********************************************************************
* Prompts the user for a line of input (using getline), 
* calls countLetters(), and displays the number of letters.
 ***********************************************************************/
int main()
{
   // You create an array in the function, don't need one here, too
   // char letter[256];
   countLetters();
   return 0;
}

/**********************************************************************
* Function to return the number of letters in a string.
 ***********************************************************************/
void countLetters()
{
   // Your method creates an array of only 2 bytes
   //char text[] = " ";
   char text[256];
   int count = 0;

   // You forgot to declare letter
   char letter;
   cout << "Enter a letter: ";
   cin >> letter;
   // Reading the char leaves a new line. Consume. cin.ignore is another way
   cin.getline(text, 256);

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

   // Overly complicated
   //for (int i = (sizeof(text) / sizeof(text[0]) - 2); i >=0; i--)
   for (int i = 0; text[i]; i++)
   {
      // Compare to letter
      if (text[i] == letter)
      {
         count++;
      }
      // This needs to be outside the loop
      //cout << "Number of '" << letter << "'s: " << count << endl;
   }
   cout << "Number of '" << letter << "'s: " << count << endl;
}

In C++, it's almost always better to use std::string instead of raw char arrays, but I'll assume this is an assignment and arrays are required.

001
  • 13,291
  • 5
  • 35
  • 66