Program to find out whether a word is a 'Magic Word' or not

sravan953 0 Tallied Votes 6K Views Share

This code snippet allows you to know whether a word/sentence you have entered is a 'Magic Word' or not.

A 'Magic Word' is a word which has at any position two consecutive letters. This can be achieved by taking the ASCII values of the characters.

For example, if I enter "all the best", the program will return that is Magic Word since in the end 's' and 't' are consecutive(ASCII value of 't'=ASCII value of 's'+1).

import java.io.*;
class magic_word
{
static void magic()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a word: ");
        String s=br.readLine();
        int counter=0;
        for(int i=0;i<s.length()-1;i++)
            {
                char a=s.charAt(i);
                char b=s.charAt(i+1);
                int x=(int)a;
                int y=(int)b;
                if(y==x+1)
                    {
                        System.out.println("Magic word!");
                        counter++;
                        break;
                    }
                }
                if(counter==0)
                    {
                        System.out.println("Not a Magic word");
                    }
                }
            }
rnthelord.2008 -2 Newbie Poster

i need a big program using only java.io & java.math & java.lang

commented: Then you have some typing to do. Get started! -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.