Start New Discussion within our Software Development Community

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");
                    }
                }
            }
The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.