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

Member #531174 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");
                    }
                }
            }

Dani AI

Generated

If by "Magic Word" you mean any input that contains at least one adjacent pair of alphabetic characters where the second is exactly the next letter (e.g., c followed by d), you can implement it in O(n) by scanning once, normalizing case, and checking only letter pairs. Using raw ASCII deltas is fine for the basic Latin alphabet; just be careful to ignore spaces and punctuation and to avoid treating digits as letters. In Java, char is UTF-16, but for A-Z/a-z the code points match ASCII, so a simple delta check is safe for this definition. See Oracle’s Character docs (Character) and ASCII background if needed (ASCII).

public static boolean isMagicWord(String s) {
    if (s == null || s.length() < 2) return false;
    for (int i = 0; i + 1 < s.length(); i++) {
        char a = Character.toLowerCase(s.charAt(i));
        char b = Character.toLowerCase(s.charAt(i + 1));
        if (Character.isLetter(a) && Character.isLetter(b) && b == a + 1) {
            return true; // found consecutive letters like 'm','n'
        }
    }
    return false;
}

Notes:

  • This is case-insensitive and only considers adjacent letters; spaces or punctuation break the pair.
  • If you want to allow skipping non-letters (e.g., a-b counts as ab), advance the right index to the next letter before comparing.
  • Do not wrap z to a unless you explicitly want that rule; it is not implied by ASCII adjacency.
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.