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

Please help me to create (fill-in) the following 6 functions (to create a set of

ID: 3675663 • Letter: P

Question

Please help me to create (fill-in) the following 6 functions (to create a set of functions to analyze strings of characters):

1. numLines - Given a string, return the number of lines of text it contains. In UNIX, lines of text are delimited by the ‘ ’ character. Note that a string of characters which does NOT include a is considered a single line. If the string ends with a ‘ ’, then since there isn’t anything after it, there isn’t a new line.

2. upperCaseChars - Given a string, return the number of uppercase characters. Use isupper from the library.

3. lowerCaseChars - Given a string, return the number of lowercase characters. Use islower from the library..

4. numPunct - Given a string, return the number of punctuation characters. Use ispunct from the library.

5. numWords - Given a string, return the number of words it contains. A word is defined to be a sequence of alphabetic (isalpha) characters with non-alphabetic characters on each side. If alphabetic characters start or end the string, they are considered a word.

6. characterCounts - Given a string, return a vector of ints of length 26 containing the number of times each letter was seen. Index 0 corresponds to ‘a’, 1 to ‘b’, etc… Treat upper-case letters lower-case.

#include <cmath>
#include <cctype>

using namespace std;

unsigned numLines(const string& st){

return 0
}

unsigned upperCaseChars(const string& st){
  
  
return 0;
}

unsigned lowerCaseChars(const string& st){
  
  
return 0;
}

unsigned numPunct(const string& st){
  
  
return 0;
}

unsigned numWords(const string& st){
  
  
return 0;
}

vector<int> characterCounts(const string& st){
vector<int> ret(26);
  
  
return ret;
}

Thank you very much!

Explanation / Answer

#include<iostream>
#include<string>
#include <cmath>
#include <cctype>
#include <vector>

#define OUT 0
#define IN 1

using namespace std;

unsigned numLines(const string& str){
   int state = OUT;
    unsigned nl = 0; // line count
   int n = str.size();
   int i=0;
    // Scan all characters one by one
    while (i < n)
    {
        // If next character is a ' ', set the
        // state as OUT
        if (str[i] == ' ' )
            state = OUT;

        // If next character is not a ' ' and
        // state is OUT, then set the state as IN and
        // increment line count
        else if (state == OUT)
        {
            state = IN;
            ++nl;
        }

        // Move to next character
        i++;
    }

    return nl;
  
}

unsigned upperCaseChars(const string& str){
int count = 0;
int n = str.size();
int i=0;
while (i < n)
{
    char c=str[i];
    if(isupper(c))
       count++;
    i++;
}

return count;
}

unsigned lowerCaseChars(const string& str){
int count = 0;
int n = str.size();
int i=0;
while (i < n)
{
    char c=str[i];
    if (islower(c)) count++;
    i++;
}

return count;
}

unsigned numPunct(const string& str){
int count = 0;
int n = str.size();
int i=0;
while (i < n)
{
    char c=str[i];
    if(ispunct(c))
       count++;
    i++;
}

return count;
}

unsigned numWords(const string& str){
   int state = OUT;
    unsigned wc = 0; // word count
   int n = str.size();
   int i=0;
    // Scan all characters one by one
    while (i < n)
    {
        // If next character is a separator, set the
        // state as OUT
        if (str[i] == ' ' || str[i] == ' ' || str[i] == ' ')
            state = OUT;

        // If next character is not a word separator and
        // state is OUT, then set the state as IN and
        // increment word count
        else if (state == OUT)
        {
            state = IN;
            ++wc;
        }

        // Move to next character
        ++i;
    }

    return wc;
}

vector<int> characterCounts(const string& str){
   vector<int> ret(26);
  
   for( unsigned int i = 0; i < str.size(); i++ ) {
       if(isalpha(str[i])){
           ret[str[i]]++;
           }
   }

   return ret;
}