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

So the prompt I\'ve been given is Write a function that counts the number of tim

ID: 3682445 • Letter: S

Question

So the prompt I've been given is

Write a function that counts the number of times two specified letters appear in a given string. Your function will take as parameters the string, and the two letters to be counted. For example, if the given string is "test string" and the letters to count are "t" and "s", the function will return 5, since there are three t's and two s's. Use the following function header:

int countLetters(string testString, char letter1, char letter2) {

}

and the code that I've written so far is

#include <iostream>
#include <string>

using namespace std;

int countLetters(string testString, char letter1, char letter2){
for (int i=0; i<testString.length(); i++){
if (letter1 == testString[i]){
return true;
}
}
return false;
  
for (int i=0; i<testString.length(); i++){
if (letter2 == testString[i]){
return true;
}
}

return false;
}

Could someone tell me where I'm going wrong?

Explanation / Answer

Java:

public static int countOccurrences(String testString, char letter1)
{
int count = 0;
for (int i=0; i < testString.length(); i++)
{
if (testString.charAt(i) == letter1||testString.charAt(i) == letter2)
{
count++;
}
}
return count;
}

C-Language:

int countLetters(string testString, char letter1, char letter2) {
int i=0;count=0
for(i=0;c[i]!='';++i)
{
if(letter1==c[i]||letter2==c[i])
++count;
}

return count;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote