In: Computer Science
In this assignment you must submit TWO different programs both of which accomplish the same thing but in two different ways.
Using NetBeans, create a program that prompts the user for a sentence. Then the program displays the position of where the lowercase letter 'a' appears everywhere in the sentence. Here is a sample of the input and output:
Enter a sentence
for the night is dark and full of terrors.
The lowercase letter 'a' appears at character position 18
The lowercase letter 'a' appears at character position
22
Write TWO different programs that accomplishes the above using different techniques. Take a look at the JavaDocs for the String methods available to you. For example, one way is you might use the 'charAt()' method while your second program uses 'indexOf()'.
please send me the solution in java as soon as possible.
Code:
Technique1: Using charAt()
import java.util.*;
class Technique1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='a')
System.out.println("The lowercase letter 'a' appears at character position "+i);
}
}
}
Technique 2: Using indexOf()
import java.util.*;
class Technique2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
String s=sc.nextLine();
int index = s.indexOf('a');
while (index >= 0) {
System.out.println("The lowercase letter 'a' appears at character position "+index);
index = s.indexOf('a', index + 1);
}
}
}
Output:
