Question

c++ Write a function Count_m_z that takes a string as an input parameter argument and counts...

c++

Write a function Count_m_z that takes a string as an input parameter argument and counts the number of m, n, o, ... z characters in it. The function returns an integer value for the number of times those 14 lowercase letters appear in the input string.

  • Your function should be named Count_m_z

  • Your function should take one string parameter

  • Your function should return the number of m, n, o, ... z characters as an integer

  • Your function should not print/output anything

Edge Cases

  • If the string is empty, return -1

  • If there are no m, n, o, ... z characters in the string, return -2

Examples

  • string "muenzinger" → return 6

  • string "mUENZINGER" → return 1

  • string "MUENZINGER" → return -2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
using namespace std;

//function to count the number of m, n, o, ... z characters
int Count_m_z(string str)
{
//declare and initialization of variable
int count = 0, length = 0;
  
//check the input string character by character
while (str[length] != '\0')
{
//check with ASCII value
//ASCII value of m is 109
//ASCII value of z is 122
if(str[length]>108 && str[length]<123)
{
count++;
}
length++;
}
  
//if string length is zero
if(length==0)
return -1;
  
//if number of m, n, o, ... z characters is zero
if(count==0)
return -2;
  
return count;
}

int main()
{
//variable declaration
string str;
str = "muenzinger";
//display the result
cout<<endl<<"The number of m, n, o, ... z characters: "<<Count_m_z(str);
return 0;
}

OUTPUT:


Add a comment
Know the answer?
Add Answer to:
c++ Write a function Count_m_z that takes a string as an input parameter argument and counts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT