how to write a program in java to print the third letter of each word and also to print the characters which are at even position in the sentence
Answers
Answered by
3
import java.util.*;
class String
{
static void main(String[] args)
{
Scanner sn = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sn.nextLine();
s=s+" ";
int len = s.length();
int p=0;
for(int i=0;i<=len-1;i++)
{
char ch = s.charAt(i);
if(ch==' ')
{
String s1 = s.subString(p,i);
p=i+1;
char a = s1.charAt(2)+ ' ';
}
if(i%2==0)
{
System.out.print("Letter at even place:" + ch);
}
}
System.out.print("3 letter of each word: " + a);
System.out.print("Sentence entered: "+ s);
}
}
class String
{
static void main(String[] args)
{
Scanner sn = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = sn.nextLine();
s=s+" ";
int len = s.length();
int p=0;
for(int i=0;i<=len-1;i++)
{
char ch = s.charAt(i);
if(ch==' ')
{
String s1 = s.subString(p,i);
p=i+1;
char a = s1.charAt(2)+ ' ';
}
if(i%2==0)
{
System.out.print("Letter at even place:" + ch);
}
}
System.out.print("3 letter of each word: " + a);
System.out.print("Sentence entered: "+ s);
}
}
Similar questions