-1

Sample Input 1:

Enter the first letter:R
Enter the second letter:A
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

Sample Output 1:

RAINBOW

Sample Input 2:

Enter the first letter:R
Enter the second letter:E
Enter the third letter:I
Enter the fourth letter:N
Enter the fifth letter:B
Enter the sixth letter:O
Enter the seventh letter:W

Sample Output 2:

The spelling is wrong

Code:

public class spellcheck {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan = new Scanner(System.in);
        String str1;
        String str2 = "rainbow";
        int i = 0;
        String a[] = new String[7];
        System.out.print("Enter the first letter:");
        a[0] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[1] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[2] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[3] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[4] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[5] = scan.nextLine();
        System.out.print("Enter the first letter:");
        a[6] = scan.nextLine();
    }
}

What should be the next step?

0

3 Answers 3

0
String tmp = "";

for(int j = 0; j < a.length; j ++)
tmp += a[j];

System.out.println(tmp);

Output -

Enter the first letter:r
Enter the first letter:a
Enter the first letter:i
Enter the first letter:n
Enter the first letter:b
Enter the first letter:o
Enter the first letter:w
rainbow

To compare you can utilize the built in String function .equals -> tmp.equals("Otherstring")

Sign up to request clarification or add additional context in comments.

Comments

0
for (int i = 0; i < a.length; i++) {
    str1 = str1 + a[i];
}

Then compare by str2.equals(str1)

Comments

0
  1. Use a char array instead of a String array for storing the letters.
  2. Create a String out of the char array and then compare the content.
  3. Use equalsIgnoreCase instead of equals to compare the spelling in a case-insensitive way.
  4. Also, always follow the Java naming conventions e.g. the name of your class should be SpellCheck as per the naming conventions.

Do it as follows:

import java.util.Scanner;

public class SpellCheck {
    public static void main(String[] args) {
        String[] suffix = { "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th" };
        Scanner scan = new Scanner(System.in);
        String str;
        String str2 = "rainbow";
        char a[] = new char[str2.length()];
        for (int i = 0; i < a.length; i++) {
            do {
                System.out.print(
                        "Enter the " + ((i + 1) + suffix[i % 100 > 9 && i % 100 < 13 ? 3 : i % 10]) + " letter: ");
                str = scan.nextLine();
                if (str.length() == 1) {
                    a[i] = str.charAt(0);
                }
            } while (str.length() != 1);
        }
        String str1 = new String(a);
        if (str1.equalsIgnoreCase(str2)) {
            System.out.println(str2.toUpperCase());
        } else {
            System.out.println("The spelling is wrong");
        }
    }
}

A sample run:

Enter the 1st letter: R
Enter the 2nd letter: A
Enter the 3rd letter: I
Enter the 4th letter: N
Enter the 5th letter: B
Enter the 6th letter: O
Enter the 7th letter: W
RAINBOW

Another sample run:

Enter the 1st letter: R
Enter the 2nd letter: E
Enter the 3rd letter: I
Enter the 4th letter: N
Enter the 5th letter: B
Enter the 6th letter: O
Enter the 7th letter: W
The spelling is wrong

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.