-6

This program counts the number of times a specific character appears in a string. The compiler shows me an incomplete output without displaying how many times the character shows in the input then closes abruptly

Enter a string (up to 50 characters): k;kl;kl;k;kljhh
Enter a character and I will tell you how many
times it appears in the string: k
k appears Press any key to continue...

I'm using VS community.

    // This program demonstrates a function, countChars, that counts
    // the number of times a specific character appears in a string.
#include <iostream>
using namespace std;

int countChars(char *, char);  // Function prototype

int main()
{
   const int SIZE = 51;    // Array size
char userString[SIZE];  // To hold a string
char letter;            // The character to count

// Get a string from the user.
cout << "Enter a string (up to 50 characters): ";
cin.getline(userString, SIZE);

// Get a character to count occurrences of within the string.
cout << "Enter a character and I will tell you how many\n";
cout << "times it appears in the string: ";
cin >> letter;

// Display the number of times the character appears.
cout << letter << " appears ";
cout << countChars(userString, letter) << " times.\n";
return 0;
 }

//****************************************************************
// Definition of countChars. The parameter strPtr is a pointer   *
// that points to a string. The parameter Ch is a character that *
// the function searches for in the string. The function returns *
// the number of times the character appears in the string.      *
//****************************************************************

int countChars(char *strPtr, char ch)
{
   int times = 0;  // Number of times ch appears in the string

   // Step through the string counting occurrences of ch.
   while (*strPtr != '\0')
    {
      if (*strPtr == ch)  // If the current character equals ch...
          times++;         // ... increment the counter
       strPtr++;           // Go to the next char in the string.
   }

    return times;
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Sasha S
  • 11
  • What error do you get? If it crashes, what stacktrace do you see in your debugger? More information needed. – Jesper Juhl Mar 24 '18 at 20:38
  • 1
    You should learn to debug your code with a debugger. If you still can't figure it out, post a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). This is not a free debugging service. http://idownvotedbecau.se/nodebugging/ – eesiraed Mar 24 '18 at 20:38
  • See [this](https://stackoverflow.com/q/454681/9254539) on how to keep the console window from closing instantly. – eesiraed Mar 24 '18 at 20:41
  • Thanks guys. Sorry I'm new here and new to c++ too. My program stays open with system("PAUSE"); before the return statement, but for some reason isn't printing the number of characters in the word even though all the code seems good to me. – Sasha S Mar 24 '18 at 20:49
  • I tried it with several online compilers and VS 2017, and it compiles and runs just fine in all of them – Hawkeye5450 Mar 24 '18 at 20:52
  • Thanks Hawkeye5350 maybe im setting up something wrong. – Sasha S Mar 24 '18 at 21:02
  • Try to pass an array of characters and its size instead: `int countChars(char pTxt[], const int size, char c);` – Raindrop7 Mar 24 '18 at 21:20
  • If you won't take time to learn your debugger, you might try instrumenting your code with debug 'cout's. In particular for this case, echo your inputs. – 2785528 Mar 24 '18 at 21:33
  • Use `cin.ignore();` after `cin.getline(userstring, size)` and after `cin >> letter; ` – Raindrop7 Mar 24 '18 at 21:39
  • Thanks Raindrop7, but i wonder why it isnt printing how many times the letter was used? This line is getting ignored: cout << countChars(userString, letter) << " times.\n"; – Sasha S Mar 24 '18 at 22:06

2 Answers2

0

Unable to reproduce on Ubuntu. Perhaps a windows issue?

#include <iostream>
#include <iomanip>

class T590_t
{
   std::stringstream ssin;

public:

   T590_t() = default;
   ~T590_t() = default;

   int exec(int , char** )
      {
         ssin << "k;kl;kl;k;kljhh\nk";  // for consistent behavior, use stringstream for input
         // the letter ------------^

         const int SIZE = 51;    // Array size
         char userString[SIZE];  // To hold a string

         // Get a string from the user.
         std::cout << "\n  Enter a string (up to 50 characters): ";
         (void)ssin.getline(userString, SIZE);
         std::cout << "\n  '" << userString << "'" << std::endl;  // echo input

         // Get a character to count occurrences of within the string.
         std::cout << "\n  Enter a character and I will tell you how many times it appears in the string: ";
         char letter = '\0';            // The character to count, initialized
         ssin >> letter;
         std::cout << "  '" << letter << "'" << std::endl;         // echo input

         // Display the number of times the character appears.
         std::cout << "\n  " << letter << " appears "
                   << countChars(userString, letter) << " times.\n";

         return 0;
      }


private: // methods

   int countChars(char *strPtr, char ch)
      {
         int times = 0;  // Number of times ch appears in the string

         // Step through the string counting occurrences of ch.
         while (*strPtr != '\0')
         {
            if (*strPtr == ch)  // If the current character equals ch...
               times++;         // ... increment the counter
            strPtr++;           // Go to the next char in the string.
         }

         return times;
      }

}; // class T590_t


int main(int argc, char* argv[])
{
   T590_t  t590;
   return  t590.exec(argc, argv);
}

with output:

  Enter a string (up to 50 characters): 
  'k;kl;kl;k;kljhh'

  Enter a character and I will tell you how many times it appears in the string:   'k'

  k appears 5 times.
2785528
  • 5,438
  • 2
  • 18
  • 20
  • I used std::stringstream initialized with user responses for consistent behavior. – 2785528 Mar 24 '18 at 21:22
  • You have marked this post as C++. I recommend you use "std::string userString;" (with a few associated changes). – 2785528 Mar 24 '18 at 21:29
  • There are several good reasons for "int countChars(char *strPtr, char ch)" to be "int countChars(const char *strPtr, char ch)". Most prominent might be that with the const any reader will quickly know your function does not modify the char * in which it is counting. – 2785528 Mar 24 '18 at 21:37
0

You can pass an array of characters and its size instead:

int GetCharCount(char[], const int, const char);


int main(){

    char text[] = "Hello there!";
    cout << GetCharCount(text, strlen(text), 'e') << endl;

    cout << endl << endl;
    cin.get();
    return 0;
}

int GetCharCount(char pTxt[], const int size, const char c){
    int count = 0;
    for(int i(0); i != size; i++)
        if(c == pTxt[i])
            count++;
    return count;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27