In: Computer Science
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"
// 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));
}
}