Computer Applications
Write a program in Java to accept a word. Pass it to a method magic(String str). The method checks the string for the presence of consecutive letters. If two letters are consecutive at any position then the method prints "It is a magic string", otherwise it prints "It is not a magic string".
Sample Input: computer
Sample Output: It is not a magic string
Sample Input: DELHI
Sample Output: It is a magic string
Java
User Defined Methods
58 Likes
Answer
import java.util.Scanner;
public class KboatMagicString
{
public void magic(String str) {
boolean isMagicStr = false;
String t = str.toUpperCase();
int len = t.length();
for (int i = 0; i < len - 1; i++) {
if (t.charAt(i) + 1 == t.charAt(i + 1)) {
isMagicStr = true;
break;
}
}
if (isMagicStr)
System.out.println("It is a magic string");
else
System.out.println("It is not a magic string");
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter word: ");
String word = in.nextLine();
KboatMagicString obj = new KboatMagicString();
obj.magic(word);
}
}
Variable Description Table
Program Explanation
Output
Answered By
26 Likes
Related Questions
Write a program using method name Glcm(int,int) to find the Lowest Common Multiple (LCM) of two numbers by GCD (Greatest Common Divisor) of the numbers. GCD of two integers is calculated by continued division method. Divide the larger number by the smaller, the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results in the GCD.
LCM = product of two numbers / GCDWrite a program in Java to accept a String from the user. Pass the String to a method Display(String str) which displays the consonants present in the String.
Sample Input: computer
Sample Output:
c
m
p
t
rWrite a program using a method called area() to compute area of the following:
(a) Area of circle = (22/7) * r * r
(b) Area of square= side * side
(c) Area of rectangle = length * breadth
Display the menu to display the area as per the user's choice.
Write a program using a method Palin( ), to check whether a string is a Palindrome or not. A Palindrome is a string that reads the same from the left to right and vice versa.
Sample Input: MADAM, ARORA, ABBA, etc.