Here is my code to put a quote down, flip it, and reverse it. I do not have much
ID: 3903403 • Letter: H
Question
Here is my code to put a quote down, flip it, and reverse it. I do not have much knowledge on coding. Does my code only use Variables, Console I/O, Java Program Design, Intro to IDEs, If Statements, Selection and Conditional Logic, Strings, Loops, Nesting, and Iteration? If not, could it be fixed to only use them?
These are the offical steps of the assignment:
- Ask a user to enter a quote - whether it's several sentences or a single word, we should be able to read in and store that quote.
- Create a loop that prints every character of the string with a space between it.
EG: "Hey there!" should print "H e y t h e r e ! "
- Create a loop that examines every letter of the quote, and results in printing it out backward. Think about loop design, and what the most efficient way to design "backward text" would be.
EG: "Hello world" should print "dlrow olleH"
- Once you have the text printing backward, modify the code so the reversed text is stored in a "backward text" string. Print the stored string instead of simply using print statements in a loop on the original string.
KEEP BOTH VERSIONS IN YOUR CODE - you should be printing the text backward twice now - once without a variable, and once with just a variable printing that holds the backward text.
- Use a loop to keep track of how many of each of the five main vowels (a, e, i, o, u) are in the original string text in total, and print a statement each time you find a vowel - you should be going in order. Once you have read the original text left to right, you should print the total vowels
EG: "apples are tasty yup" has 3 a's and 2 e's and 1 u, should print a statement pattern similar to:
a found
e found
...
u found
Text has the following vowel counts: a = 3, e = 2, i = 0, o = 0, u = 1
- After you have your vowel counts, divide them by the length of the quote to retrieve the average of each vowel in the quote.
EG: Text has the following vowel average: a = 15%, e = 10%, i = 0%, o = 0%, u = 5%
- Put all of the above work you've done into a loop that will allow you to repeatedly enter quotes. After the first time of entering quotes and doing all of the actions above, ask the user if they would like to enter another quote. If a user enters 1, allow the loop to repeat. Terminate the loop if the user enters 0, to stop typing quotes. Inform the user of the action expected with a print statement, such as: Please enter 1 to enter another quote, or 0 to exit.
Here is my code:
import java.util.*;
class Quote
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
//n is the number of test case u can have enter any number of string
int n=sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++)
{
System.out.println("Enter the string");
String str=sc.nextLine();
System.out.println("The entered string is "+ str);
//len is the variable that holds the length of the entered strinng
int len=str.length();
System.out.println("The entered string with the space in between the each character");
for(int j=0;j<str.length();j++)
{
System.out.print(str.charAt(j)+" ");
}
System.out.println("the reverse of the given string is");
for(int j=str.length()-1;j>=0;j--)
System.out.print(str.charAt(j));
System.out.println();
for(int j=0;j<str.length()-1;j++)
{
if(str.charAt(j)=='a' || str.charAt(j)=='e'|| str.charAt(j)=='i'||str.charAt(j)=='o'||str.charAt(j)=='u')
{
System.out.println(str.charAt(j)+" is found");
}
}
int counta=0,counte=0,counti=0,countu=0,counto=0;
for(int j=0;j<str.length();j++)
{
if(str.charAt(j)=='a')counta++;
else if(str.charAt(j)=='e')counte++;
else if(str.charAt(j)=='i')counti++;
else if(str.charAt(j)=='o')counto++;
else if(str.charAt(j)=='u')countu++;
}
System.out.println("The count of a is "+counta);
System.out.println("The count of e is "+counte);
System.out.println("The count of i is "+counti);
System.out.println("The count of o is "+counto);
System.out.println("The count of u is "+countu);
}
}
}
Explanation / Answer
import java.util.*;
public class Quote{
public static void main(String args[]){
Scanner sc= new Scanner(System.in); //scanner for user input
int con=0; //variable to allow user to enter another quote
do {
System.out.println("Enter the Quote: ");
String quote; //variable to store quote
quote=sc.nextLine(); //store user quote
int len=quote.length(); //store length of quote
String rev=""; //store reversed string
int counta=0,counte=0,counti=0,countu=0,counto=0; //declare & initialize variables to store count of vowels in quote
System.out.println("The entered quote with the space in between the each character : ");
for(int j=0;j<len;j++){
if(quote.charAt(j)!=' ')
System.out.print(quote.charAt(j)+" "); //avoid multiple spaces between words if not required remove IF condition
//count the vowels " a, e, i, o, u" in quote
if(quote.charAt(j)=='a')counta++;
else if(quote.charAt(j)=='e')counte++;
else if(quote.charAt(j)=='i')counti++;
else if(quote.charAt(j)=='o')counto++;
else if(quote.charAt(j)=='u')countu++;
}
System.out.println();
System.out.println("Reverse of the given quote as each character : ");
for(int j=len-1; j>-1; j--) { //print string in reverse
System.out.print(quote.charAt(j));
rev+=quote.charAt(j); //storing reversed string
}
System.out.println();
System.out.println("Reverse of the given quote stored in single variable : ");
System.out.println(rev);
System.out.println();
//printing vowels found in given quote
System.out.println("Vowels in the given Quote : ");
for(int j=0; j<len; j++){
if(quote.charAt(j)=='a' || quote.charAt(j)=='e'|| quote.charAt(j)=='i'|| quote.charAt(j)=='o'|| quote.charAt(j)=='u'){
System.out.println(quote.charAt(j)+" is found");
}
}
//Number of each vowel found in the given quote
System.out.println();
System.out.println("The count of 'a' is : "+counta);
System.out.println("The count of 'e' is : "+counte);
System.out.println("The count of 'i' is : "+counti);
System.out.println("The count of 'o' is : "+counto);
System.out.println("The count of 'u' is : "+countu);
System.out.println();
//declare & initialize variables for average of vowels in the quote
int aavr=0, eavr=0, iavr=0, oavr=0, avr=0;
//printing average of vowels in the quote
System.out.println("Average of vowel 'a' is : "+counta*100/len+"%");
System.out.println("Average of vowel 'e' is : "+counte*100/len+"%");
System.out.println("Average of vowel 'i' is : "+counti*100/len+"%");
System.out.println("Average of vowel 'o' is : "+counto*100/len+"%");
System.out.println("Average of vowel 'u' is : "+countu*100/len+"%");
System.out.println();
System.out.print("Please enter 1 to enter another quote, or 0 to exit : ");
con=sc.nextInt();
System.out.println(sc.nextLine()); //to remove buffer stored in scanner
}while(con!=0);
System.out.println("User not allowed to enter any more Quotes.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.