Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Word Reversal

Question: Given a statement, reverse its word from the last and print

Sample Input 1: Sample Input 2:


Good Bye Cruel World Is everything a joke to you?

Sample Output 2:
Sample Output 1:
you? to joke a everything Is
World Cruel Bye Good

Strings
Word Reversal
import java.util.Scanner;
public class WordReversal
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] split = s.split(" ");
String result = "";
for (int i = split.length - 1; i >= 0; i--) {
result += (split[i] + " ");
}
System.out.println(result.trim());
}
} Strings
Reversal of Character by word
Question: Given a statement, reverse it by word from the last and print

Sample Input 1: Sample Input 2:


Good Bye Cruel World Is everything a joke to you?

Sample Output 2:
Sample Output 1:
sI gnihtyreve a ekoj ot ?uoy
dooG eyB leurC dlroW

Strings
Reversal of Character by word
import java.util.Scanner;
public class CharacterReversalByWord
{
public void reverseByWord(String str){
String[] words = str.split(" ");
String finalString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--)
{
reverseWord = reverseWord + word.charAt(j);
}

Strings
Reversal of Character by word
finalString = finalString + reverseWord + " ";
}
System.out.println(finalString);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
CharacterReversalByWord obj = new CharacterReversalByWord();
String s = sc.nextLine();
obj.reverseByWord(s);
}
}

Strings
Finding a word

Question: Given a statement, find the given word, print its index. Else, print “not found”

Sample Input 1: Sample Input 2: Sample Input 3:


Good Bye Cruel World Is everything a joke to you? Is everything a joke to you?
Good to every

Sample Output 2: Sample Output 2:


Sample Output 1: 21 3
0

Strings
Finding a word
import java.util.Scanner;
public class FindAWord
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String OrigStr = sc.nextLine();
String search = sc.nextLine();
int intIndex = OrigStr.indexOf(search);
if(intIndex == - 1) {
System.out.println("not found");
}
else {
System.out.println(intIndex);
}
} Strings
Anagram Finder

Question: Given 2 statements, find if they are anagrams or not

Sample Input 1: Sample Input 2: Sample Input 3:


serve Serve Tit for tat
verse Verse Tat for tit

Sample Output 2: Sample Output 2:


Sample Output 1: No Yes
Yes

Strings
Anagram Finder
import java.util.Scanner;
import java.util.Arrays; str_1 = Arrays.toString(str_array1);
str_2 = Arrays.toString(str_array2);
import java.util.Objects; if(Objects.equals(str_1,str_2))
public class Anagram System.out.print("Yes");
else
{
System.out.print("No");
public static void main(String args[]) }
{ }

Scanner in = new Scanner(System.in);


String str_1 = in.nextLine();
String str_2 = in.nextLine();
char str_array1[] = str_1.toCharArray();
char str_array2[] = str_2.toCharArray();
Arrays.sort(str_array1);
Arrays.sort(str_array2);
Strings
Anagram Finder

Question: Given a statement, sort it alphanumerically

Sample Input 1: Sample Input 2:


kl638udbc90 onaA670ncgs8

Sample Output 2:
Sample Output 1:
Aacgnnos0678
bcdklu03689

Strings
Anagram Finder
import java.util.Scanner; for(int i=0;i<converted_array.length;i++)
import java.util.Arrays; {
if(Character.isDigit(converted_arr
class AlphaNumericSorting
{
{ System.out.print(converted_arr
public static void main(String args[]) }
}
{
Scanner in = new Scanner(System.in); }
}
String input = in.nextLine();
char converted_array[] = input.toCharArray();
Arrays.sort(converted_array);
for(int i=0;i<converted_array.length;i++)
{
if(Character.isLetter(converted_array[i]))
{
System.out.print(converted_array[i]);
Strings
}
Armstrong Numbers

Question: Find if the given number is Armstrong number. If yes, print “yes” else, “no”.

Sample Input and Output

Input: 153
Output: yes

Input: 213
Output: no
Armstrong Numbers
if(result == n)
import java.util.Scanner; System.out.println("yes");
public class Armstrong { else
public static void main(String[] args) { System.out.println("no");
}
int n, origN, rr, result = 0;
}
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
origN = n;
while (origN != 0)
{
rr = origN % 10;
result += Math.pow(rr, 3);
origN /= 10;
}
Lucas Numbers
Question: Print the first n Lucas numbers.
Lucas numbers are similar to Fibonacci numbers. Lucas numbers are also defined
as the sum of its two immediate previous terms. But here the first two terms are 2
and 1 where as in Fibonacci numbers the first two terms are 0 and 1 respectively.

Sample Input and Output

Input: 5
Output: 2 1 3 4 7

Input: 10
Output: 2 1 3 4 7 11 18 29 47 76
Lucas Numbers
import java.util.Scanner; else if (n == 1)
System.out.print("2");
public class Example7 {
public static void main(String[] args) { else
System.out.print("Input a positive number.");
Scanner sc = new Scanner(System.in); }
int n = sc.nextInt(); }
int n1 = 2, n2 = 1, n3;
if (n > 1){
System.out.print("2 1 ");
for(int i = 2; i < n; ++i){
n3 = n2;
n2 += n1;
n1 = n3;
System.out.print(n2 +" ");
}
}

You might also like