![]() |
| linear search in Java with multiple values |
Linear search in Java
Linear search in Java: Java program to search name and print name and its corresponding other value by Linear Search
Java Tutorial, Learn Java Core Java, Python tutorial and tricks, Motivational Quotes, Interview Question Answer
![]() |
| linear search in Java with multiple values |
Linear search in Java: Java program to search name and print name and its corresponding other value by Linear Search
The Floyd’s triangle is a triangle of natural consecutive numbers and it is starting with a 1 to n. it is named after Robert Floyd an American computer scientist.
It looks like the following pattern
1
2 3
4 5 6
7 8 9 10
11 12 113 14 15
import java.util.*;
public class
FloydTriangle
{
public static void main(String args[])
{
int rows; // input number of rows
int i, j; // i and j used as loop
int k = 1; // k generate the floyd's
Triangle
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of
rows to generate Floyd's Triangle:");
rows = sc.nextInt();
// loop
for(i=1; i<=rows; i++)
{
for(j=1; j<=i; j++)
{
// print the values of triangle
System.out.print(k + "
");
k++;
}
System.out.println();
}
}
}
Output:
Enter number of rows
to generate Floyd's Triangle: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Here another output
with 10 rows
Enter the number of rows
to generate Floyd's Triangle: 10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36
37 38 39 40 41 42 43
44 45
46 47 48 49 50 51 52
53 54 55
We can also print with
5 rows Floyd’s triangle as
15
14 13
12 11 10
9 8 7 6
5 4 3 2 1
By just changing k=1
to k=15 and in the loop k++ to k—as below
for(i=1; i<=rows;
i++)
{
for(j=1; j<=i; j++)
{
// print the values of triangle
System.out.print(k + "
");
k--;
}
System.out.println();
}
import java.util.Scanner;
public class LongestWordinASentence
{
public static void main(String
args[])
{
int len; // length of the string
int i; // for loop
int max=-999; // for maximum noumber
int wlen; // individual word length
char ch; // for character
String word =
""; // storing words
String longWord =
"" ; // storing longest word
Scanner in = new
Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str =
in.nextLine();
//Add space at end of
string
str=str + " ";
// length of the sentence
is stored in len
len = str.length();
for(i=0; i<len; i++)
{
ch=str.charAt(i);
// add character in
word till space encounter
if(ch != ' ')
{
word= word + ch;
}
else
{
wlen=word.length();
// check word
length with max=-999 first
// if maximum
assign max=word length
// assign the
word to longWord string variable
if(wlen>max)
{
max=wlen;
longWord=word;
}
// initialize the
word
word="";
}
}
// print the longest word
with it's length
System.out.println("The longest word is " + longWord + "
has length " + max);
}
}// end of the program
Output:
Enter a word or sentence:
everybody should come to the party
The longest word is everybody has length 9
In the following, we have replaced all words ‘red’ with ‘blue’ in the given
sentence. In this program extract the word and check each word with ‘red’, if ‘red’
then change to ‘blue’.
public class ReplaceWord
{
public static void
main(String args[])
{
// sentence taken in st
variable
String st="the red
bottle contain red water keep on a red table";
String word="";
// word taken from the sentence
String newSt="";//
New sentence form with blue word istead of red
char ch;
int l, i;
st=st + " ";
l=st.length();
for(i=0; i<l; i++)
{
ch=st.charAt(i);
// extract word
if(ch!=' ')
{
word=word+ch;
}
else
{
// checking word
red, then change to blue
if(word.equals("red"))
{
word="blue"; // assign blue
newSt=newSt +
word + " ";
word="";
}
newSt=newSt +
word + " ";
word="";
}
}
System.out.println(st);
System.out.println(newSt);
}
}
Output:
the red bottle contains red water keep on a red table
the blue bottle contains
blue water keep on a blue table
This program takes one sentence and extracts each word and counts its
vowels and print each word with their respective vowels.
import java.util.*;
public class EachWordsVowelPrint
{
public static void
main(String args[])
{
String st,
word="";
char ch,ch1;
int v; // variable for vowel
int c; // variable for Consonants
int w; // variable for word
int i,j; // for loop
int l,l1; // l is for length of sentence and for word
v=c=w=0; // Initialization
Scanner sc=new
Scanner(System.in);
System.out.println("Enter a sentence:");
st=sc.nextLine();
st=st + "
"; // one space add to the
sentence
//st=st.toUpperCase();
l=st.length(); // length of the sentence
for(i=0; i<l; i++)
{
ch=st.charAt(i);
// if space is come
then goto else calculate each words vowel
if(ch!=' ')
{
word=word + ch;
}
else
{
l1=((word.trim()).length()); //
length of the word
for(j=0; j<l1;
j++)
{
ch1=word.charAt(j);
switch(ch1)
{
case 'A'
:
case 'a'
:
case 'E'
:
case 'e'
:
case 'I'
:
case 'i'
:
case 'O'
:
case 'o'
:
case 'U'
:
case 'u'
:
v++;
}
}
// print the word
total vowels
System.out.println(word + " contain vowel= " + v);
// Initialize the
words and v for vowel
word =
"";
v=0;
}
}
}
}
Output:
Enter a sentence:
education is the best policy
education contain vowel= 5
is contain vowel= 1
the contain vowel= 1
best contain vowel= 1
policy contain vowel= 2
public class CapitalizeEachWord
{
public static void
main(String args[])
{
String st="the quick
brown dog jumps over a lazy hungary fox";
String newSt
=""; // each word capitalize the it form a sentence
String word
=""; // word taken from the
sentence
String newWord; // new word with first letter capital
char ch;
int l, i;
st=st + " ";
l=st.length();
for(i=0; i<l; i++)
{
ch=st.charAt(i);
if(ch!=' ')
{
word=word+ch;
}
else
{
// convert word
first letter capital
newWord=Character.toUpperCase(word.charAt(0)) + word.substring(1);
newSt=newSt +
newWord + " ";
word="";
}
}
System.out.println(st);
System.out.println(newSt);
}
}
Output:
the quick brown dog jumps over a lazy hungry fox
The Quick Brown Dog Jumps Over A Lazy Hungry Fox
public class TotalVowelConsonantWords
{
public static void
main(String args[])
{
String st="Education
is must for all";
char ch;
int i, l, c,sp, con,v1,
v2, v3, v4, v5;
// Initialisation of
variables
v1 = v2 = v3 = v4 = v5 =
c = sp = 0;
l=st.length();
for(i=0;i<l;i++)
{
ch=st.charAt(i);
switch(ch)
{
case 'A':
case 'a':
v1++; // vowel a count
break;
case 'E':
case 'e':
v2++; // vowel e count
break;
case 'I':
case 'i':
v3++; // vowel i count
break;
case 'O':
case 'o':
v4++; // vowel o count
break;
case 'U':
case 'u':
v5++; // vowel u count
break;
}
// Calculate total
Space for count words
if(ch==' ')
sp++;
}
// consonants = length of
String - (total vowels + space)
System.out.println("Sentence " + st);
con = l - (v1 + v2 + v3 +
v4 + v5 + sp);
System.out.println("Total String Length = " + l);
System.out.println("Total Vowel A= " +v1);
System.out.println("Total Vowel E= " +v2);
System.out.println("Total Vowel I=
" +v3);
System.out.println("Total Vowel O= " +v4);
System.out.println("Total Vowel U= " +v5);
System.out.println("Total Words = " +(sp+1)); // total space
plus 1
System.out.println("Total Consonants = "+ con);
}
}
Output:
Sentence Education is must for all
Total String Length = 25
Total Vowel A= 2
Total Vowel E= 1
Total Vowel I= 2
Total Vowel O= 2
Total Vowel U= 2
Total Words = 5
Total Consonants = 12