Question

In: Computer Science

I. Answer part A,B, C and D. 1a) Count the number of times a given char...

I. Answer part A,B, C and D.

1a)

Count the number of times a given char occurs in a given range of a String parameter (i.e. starting at a given start index, and up to but not including and end index). If end is greater than the length of the String, then the method should stop looking at the end of the String.


countCharsInRange("java", "v", 1, 2) → 0
countCharsInRange("java", "v", 0, 2) → 0
countCharsInRange("java", "v", 0, 3) → 1

1b)

Return a new String that removes all copies of the given char ch from the given String

removeChar("dog", "o") → "dg"
removeChar("cat", "o") → "cat"
removeChar("aaa", "a") → ""

1c)

Return the number of times the letter 'a' is directly followed by the letter 'b' in the given String.


countAB("abc") → 1
countAB("wxyz") → 0
countAB("bcaca") → 0

1d)

Return a new String that is a copy of the given String with each character copied the given number of times. Assume that n is 0 or more.


eachCharNTimes("abc", 2) → "aabbcc"
eachCharNTimes("xyz", 3) → "xxxyyyzzz"
eachCharNTimes("a", 5) → "aaaaa"

Solutions

Expert Solution

// JAVA program to count occurrences 
// of a character 

class Main
{ 
        // Method that return count of the given in given range
        // character in the string 
        public static int countCharsInRange(String s, char c,int st,int en) 
        { 
                int res = 0; 
     
        if(en>s.length()){
            en=s.length();
        }
                for (int i=st; i<en; i++) 
                { 
                        // checking character in string 
                        if (s.charAt(i) == c) 
                        res++; 
                } 
                return res; 
        } 
        
        // Driver method 
        public static void main(String args[]) 
        { 
                String str= "java"; 
                char c = 'v'; 
        
                System.out.println("counting character 'v' in java in the range of 1,2 is :"+countCharsInRange(str,c,1, 2));
                System.out.println("counting character 'v' in java in the range of 0,2 is :"+countCharsInRange(str,c,0, 2));
                System.out.println("counting character 'v' in java in the range of 0,3 is :"+countCharsInRange(str,c,0, 3));
                
        } 
} 

------------------------------------------------------------------------------------------------------------------------------------------------------------

  

class Main
{ 
        // Method that return new string after removing specific character of the given 
  // character in the string 
        public static String removeChar(String s, char c) 
        { 
        String newstr=""; 
     
                for (int i=0; i<s.length(); i++) 
                { 
                        // checking character in string 
                        if (s.charAt(i) != c) 
                newstr+=s.charAt(i); 
                } 
                return newstr; 
        } 
        
        // Driver method 
        public static void main(String args[]) 
        { 
                
        
                System.out.println("new string  is :"+removeChar("dog", 'o'));
                System.out.println("new string is :"+removeChar("cat",'o'));
                System.out.println("new string is :"+removeChar("aaa",'a'));
                
        } 
} 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// JAVA program to count occurrences 
// of a character 

class Main
{ 
        // Method that returns the count of a followed by b in given string
        public static int countAB(String s) 
        { 
int c=0;
     
                for (int i=0; i<s.length()-1; i++) 
                { 
                        // checking character in string 
                        if (s.charAt(i) == 'a' && s.charAt(i+1)=='b') {
                            c++;
                        }
                 
                } 
                return c; 
        } 
        
        // Driver method 
        public static void main(String args[]) 
        { 
                
        
                System.out.println("count of a follwed by b  is :"+countAB("abc"));
                System.out.println("count of a follwed by b  is :"+countAB("wxyz"));
                System.out.println("count of a follwed by b  is :"+countAB("bcaca") );
                
        } 
} 

---------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;
public class Main 
{public static String eachCharNTimes(String s,int n){
String newstr="";
    for(int i=0;i<s.length();i++){
//storing n times in a new string
        for(int j=0;j<n;j++){
            newstr+=s.charAt(i);
        }
        
        
    }
    
    return newstr;
    
    
    
}
    public static void main(String[] args) 
    {
        //calling and printing result
 
        System.out.println( eachCharNTimes("abc", 2));
        System.out.println( eachCharNTimes("xyz", 3) );
        System.out.println( eachCharNTimes("a", 5));
        
    }
}


Related Solutions

I. Answer part A, B, and C 1a) Return true if the given int parameter is...
I. Answer part A, B, and C 1a) Return true if the given int parameter is both positive and less than 10, and false otherwise. Remember that 0 is not positive! positiveLessThan10(10) → false positiveLessThan10(9) → true positiveLessThan10(11) → false 1b) Return true if the parameters are in ascending order, from left to right. Two values that are equal are considered to be in ascending order. Remember, you can just type return true or return false to return true or...
Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d)....
Part 1: answer (a), (b), (c), and (d). Part 2: answer (a), (b), (c), and (d). Godspeed, and good luck!!! CC11 Cookie Creations Natalie and her friend Curtis Lesperance decide that they can benefit from joining Cookie Creations and Curtis’s coffee shop. In the first part of this problem, they come to you with questions about setting up a corporation for their new business. In the second part of the problem, they want your help in preparing financial information following...
III. Answer part A,B, and C. (in JAVA) 3a) Return the number of times the letter...
III. Answer part A,B, and C. (in JAVA) 3a) Return the number of times the letter 'a' occurs in a String. countA("abc") → 1 countA("wxyz") → 0 countA("bcaca") → 2 3b) Return the number of times the given letter ch occurs in the given String str. countNumChar("abc", "a") → 1 countNumChar("wxyz", "b") → 0 countNumChar("bcaca", "c") → 2 3c) Count the number of characters that are either a or b in a String. Try to use only one for loop....
ONLY ANSWER PART C, D AND E 1A. Suppose The lifetime for a competing brands of...
ONLY ANSWER PART C, D AND E 1A. Suppose The lifetime for a competing brands of tires are independent of each other and approximately normal with unknown means but known variances σ^2= 975 miles^2 and σ^2= 965 miles^2. We gather did some testing under controlled conditions and have the following summary statistics: n1= 75 ̄x1= 3251.4 n2= 60 ̄x2= 3274.7 Construct a 80% confidence interval for the difference in population means,μ1−μ2. B.Using the same setup as part A, conduct a...
Fix the C++ code to count the number of inversions using the given array. The answer...
Fix the C++ code to count the number of inversions using the given array. The answer is 8 inversions, but I am only getting 6 inversions. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; int mergeInversion(int arr[], int l, int m, int r) {     // size of the two arrays     int n1 = m - l + 1;     int n2 = r - m;     // create temporary arrays     int L[n1];     int R[n2];     // Copy the...
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
I am running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
Given the operation times provided: JOB TIMES (minutes) A B C D E F Center 1...
Given the operation times provided: JOB TIMES (minutes) A B C D E F Center 1 20 16 43 60 35 42 Center 2 27 30 51 12 28 24 a. Develop a job sequence that minimizes idle time at the two work centers. The sequence is             (Click to select)  A-B-C-D-E-F  B-A-C-E-F-D  C-A-B-D-E-F  F-E-D-A-B-C  E-F-D-B-C-A  D-C-B-A-E-F  . b. Determine idle time of center 2, assuming no other activities are involved. Idle time             minutes
If I show (A and (B → C)) → D and (A and (C → B))...
If I show (A and (B → C)) → D and (A and (C → B)) → D, can I conclude A → D?
I have A B and C answered I only need the answer to D and E....
I have A B and C answered I only need the answer to D and E. I have included the correct answers for A B and C Problem 8-41 (LO. 2, 3, 9) Lori, who is single, purchased 5-year class property for $200,000 and 7-year class property for $400,000 on May 20, 2016. Lori expects the taxable income derived from her business (without regard to the amount expensed under § 179) to be about $800,000. Lori wants to elect immediate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT