![maria5633](/avatars/41024.jpg)
maria5633
24.03.2020 •
Computers and Technology
You have a String instance of finite length consisting of only 'a' and 'b's in it. Write the following solution class with the exact target function to separate all of the 'a' and 'b' in the string so that all 'a' appear before 'b' in it:
Solved
Show answers
More tips
- F Family and Home Protect Your Home or Apartment from Pesky Ants...
- O Other What is a Disk Emulsifier and How Does it Work?...
- H Health and Medicine How to Calm Your Nerves? Expert Tips That Actually Work...
- A Animals and plants 5 Tips for Taking Care of Yews to Keep Them Green and Beautiful...
- S Sport How to wrap boxing hand wraps? Everything you need to know!...
- F Food and Cooking 10 Reasons Why You Should Avoid Giving Re-Gifts: An Informative Guide...
- F Family and Home Tender Care for Your Parquet: Is it Possible to Clean Parquet?...
- S Style and Beauty How Are Eyelash Extensions Applied? All Your Questions Answered...
- F Food and Cooking 10 Tips for Proper Sushi Consumption...
- S Style and Beauty Learn how to tie a keffiyeh on your head like a pro...
Answers on questions: Computers and Technology
- C Computers and Technology How to get the electronic version of insurance card from geico...
- C Computers and Technology B13 TYPOGRAPHY Assignment: Watch the approximate 45 minute video on TYPOGRAPHY (links to clips below). After-which, answer the following questions OR complete the requested...
- P Physics M A 1.0 kg cart moving right at 5.0 on a frictionless track S m collides with a 3.0 kg cart moving left at 2.0 1.0 kg cart has a final speed of 4.0 to the left. . The S...
- M Mathematics What is the answer to 11/12 3/4...
- H History Which two regions are most alike in terms of physical features? a. Intermountain and Appalachian Mountains b. Rocky Mountains and Appalachian Mountains c. Intermountain...
- E Engineering OOO What actions can you take from the Go To tab? Check all that apply. moving to a specific page moving to a specific line moving a file to a new folder O moving a specific...
- M Mathematics Which is greater 0 or /-9/...
- H History ANSWER NOW The main reason Yemen ranks lowest in the world in gender equality is because ΟΟΟΟ men make all the major decisions. women make up less than 1 percent of parliament....
- M Mathematics What is the name of the property of multiplication shown the equation. a - zero property b - associate property c - commutative property d - identity property...
- H History 3 modern day countries that used to be apart of the British empire in Africa...
Ответ:
Required code and output is given below:
Explanation:
import java.util.Scanner;
public class HomworkAssignment3_1
{
public static void main(String[] args)
{
String str;
Scanner console=new Scanner(System.in);
System.out.printf("Input: ");
//read input from user
str=console.nextLine();
//check if length of the string is above 50
if(str.length()>50)
{
System.out.println("Input string size must be less than 50.");
}
//check if string is null or empty
else if(str==null || str.equals(""))
{
System.out.println("Input string should not be null or empty.");
}
//call method isvalid that takes string,str is not true
else if(!isvalid(str))
{
System.out.println("Input string must contains only a and b letters");
}
else
{
//create an instance of Solution
Solution solution=new Solution();
//call the method,separateLetters
System.out.println("Output: "+solution.separateLetters(str));
}
}
/*The method, isvalid that takes string and
* return true if the str is valid otherwise
* returns false.*/
public static boolean isvalid(String str)
{
boolean valid=true;
for (int i = 0; i < str.length() && valid; i++) {
if(str.charAt(i)!='a' && str.charAt(i)!='b')
valid=false;
}
return valid;
} //end of the method isvalid
} //end of the class
//Solution.java
public class Solution
{
/*The method that sorts the */
public String separateLetters(String input)
{
//convert the string to character array
char ch[]=input.toCharArray();
//apply sorting algorithm
for (int outer = 0; outer < input.length(); outer++)
{
for (int inner = 0; inner < input.length()-outer-1; inner++)
{
if(ch[inner]>ch[inner+1])
{
char temp=ch[inner];
ch[inner]=ch[inner+1];
ch[inner+1]=temp;
}
}
}
//returns the sorted string
return new String(ch);
}
}
Sample output:
Run1:
Input: bababa
Output: aaabbb
Run2:
Input: a
Output:
Ответ:
int number;
for (int i=0; i<10; i++) {
printf("Enter an integer number: ");
scanf_s("%d", &number);
sum += number;
}
printf("The sum is %d", sum);