Question

Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)

please do in c++


Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: 

count7s(717) → 2 

count75(7) →1 

count7s(123) → 0 


Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or more characters at the very beginning of the given string, and at the very end of the string in reverse order (possibly overlapping). For example, the string "abXYZba" has the mirror end "ab". 

mirrorEnds("abXYZba") → "ab" 

mirror Ends("abca") → "a" 

mirror Ends("navan") → "navan" 


Q6. Write a C++ function of the form int wordsCount(string[], int, int), where the first int is the size of the string array and the second int is the length (in characters) of the strings we wish to count. So, given an array of strings, return the count of the number of strings with the given length. 

wordsCount({"a", "bb", "b", "ccc"}, 4, 1) → 2 

wordsCount({"a", "bb", "b", "ccc"}, 4, 3) → 1 

wordsCount({"a", "bb", "b", "ccc"}, 4, 4) → 0

2 0
Add a comment Improve this question Transcribed image text
Answer #1

Q4)

CODE:

#include
using namespace std;
int count7s(int n){
   if(n==0)
       return 0;
   if(n%10==7)
       return 1+count7s(n/10);
   return count7s(n/10);  
}
int main(){
   cout<<count7s(717)<<endl;
   cout<<count7s(7)<<endl;
   cout<<count7s(123)<<endl;
}

OUTPUT:

Q5)

CODE:

#include
using namespace std;
void mirrorEnds(string s){
   int n=s.size();
   int end=n-1,i;
   for(i=0;i<n/2;i++){
       if(s[i]==s[end--])
           cout<<s[i];
       else
           return ;
   }
   if(i==n/2)
   {
       for(int j=i;j<n;j++)
       {
           cout<<s[j];
       }
   }
}
int main(){
   mirrorEnds("abXYZba");
   cout<<endl;
   mirrorEnds("abca");
   cout<<endl;
   mirrorEnds("navan");
}

OUTPUT:

Q6)

CODE:;

#include
using namespace std;
int wordsCount(string s[],int n,int len){
   int count=0;
   for(int i=0;i< n;i++){
       if(s[i].size()==len)
           count++;
   }
   return count;
}
int main(){
string s[]={"a","bb","b","ccc"};
   cout<<wordsCount(s,4,1) < < endl;
   cout<<wordsCount(s,4,3) < < endl;
   cout<<wordsCount(s,4,4) < < endl;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)
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
  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

  • c++ int count(const string& str, char a); Write a recursive function that finds the number of...

    c++ int count(const string& str, char a); Write a recursive function that finds the number of occurrences of a specified letter in a string. For example,count("Welcome",’e’)returns 2.

  • C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "c" "" "b" "a" "d" "e" Write a function, int flip(string a[], int n); It reverses the order of the elements of...

  • Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...

    Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. 1. write the code using function 2. Please try to implement a function after the main function and provide prototype before main function. Total Characters in String Array 10 points Problem 2 Declare a string array of size 5. Prompt the user enter five strings that are...

  • 8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the...

    8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct. The program you are going to write will produce a String containing a tabular representation of a 2D String array. For the 2D arrays, assume that the first...

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

  • Write a value-returning C++ function returns the average length of an array of strings. Name the...

    Write a value-returning C++ function returns the average length of an array of strings. Name the function stringsAverageLength and use the following header: double stringsAverageLength(string array [], int n) { } where the parameter 'array' has 'n' strings and the return value is the average length of all of the strings in the array. For example, if the function is called like this: string cars[3] = { "Toyota", "Ford", "Tesla" }; cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) <<...

  • write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

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
Active Questions
ADVERTISEMENT