Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(c++) My assignment requires me to count the number of instances of a character

ID: 3573022 • Letter: #

Question

(c++) My assignment requires me to count the number of instances of a character (letter) shows up in a sentence, my implementation of this problem so far is..

#include <iostream>

using namespace std;

void IN(char sen[80]);
void Search(char s[80]);

main()
{
   char sentence[80];
   IN(sentence);
}

void IN(char sen[80])
{
   cout << "Type in a sentence" << endl;
   cin >> sen;
   Search(sen);
}

void Search(char s[80])
{
   int numberofinstances = 0;
   char input;
   cout << "Which letter character are you searching multiple instances of?" << endl;
   cin >> input;

for(int i = 0; i < 80; i++)
{
   if(input == s[i])
   {
     numberofinstances++;
   
    cout << "there are" << " " << numberofinstances << " " << "instances of" << " " << input << endl;
   }
}
}

Explanation / Answer

Hi,

I have updated the code. It is working fine as expected. Highlighted the code changes below.

#include <iostream>
using namespace std;
void IN(string sen);
void Search(string s);

main()
{
string sentence;
IN(sentence);

}
void IN(string sen)
{
cout << "Type in a sentence" << endl;
getline(cin , sen);
Search(sen);
}
void Search(string s)
{
int numberofinstances = 0;
char input;
cout << "Which letter character are you searching multiple instances of?" << endl;
cin >> input;
for(int i = 0; i < s.length(); i++)
{
if(input == s[i])
{
numberofinstances++;


}
}
cout << "there are" << " " << numberofinstances << " " << "instances of" << " " << input << endl;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                   

sh-4.2$ main                                                                                                                                                                                                                                           

Type in a sentence                                                                                                                                                                                                                                     

This is c++. Dealing with strings now                                                                                                                                                                                                                  

Which letter character are you searching multiple instances of?                                                                                                                                                                                        

e                                                                                                                                                                                                                                                      

there are 1 instances of e                                                                                                                                                                                                                             

sh-4.2$ main                                                                                                                                                                                                                                           

Type in a sentence                                                                                                                                                                                                                                     

This is c++. Dealing with strings now                                                                                                                                                                                                                  

Which letter character are you searching multiple instances of?                                                                                                                                                                                        

s                                                                                                                                                                                                                                                      

there are 4 instances of s