|
I have to write a program that asks the user to enter in a string then tells you if it is a panagram (includes all the alphabet), if not displays the letters missing.
Where do I start.
I have written the following which works ok untill you switch the order of the characters then it dont find it.
import java.lang.*;
import java.util.*;
public class pangram {
public static void main(String[] args) {
String name ="abcdefgihjklmnopqrstuvwxyzafd";
int index = name.indexOf("abd"); // 1
System.out.print("hello");
if (index<0)
{System.out.print("Not found");}
if (index>=0)
{System.out.print("Found");}
}
}
|
|
|
public class PangramTest
{
static String domain = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args)
{
String[] testStrs =
{
"hello world",
"the five boxing wizards jump quickly",
"the quick brown fox jumps over a lazy dog"
};
for(int j = 0; j < testStrs.length; j++)
System.out.println(isPangram(testStrs[j]));
}
private static boolean isPangram(String s)
{
// run through domain and look for each letter in s
// if we find them all, return true
// if we miss one, return false
char[] chars = domain.toCharArray();
for(int j = 0; j < chars.length; j++)
{
char next = chars[j];
if(s.indexOf(next) == -1)
{
System.out.println("couldn't find " + next);
return false;
}
}
return true;
}
}
|
|
|
|
|
Hi, I compiled the program but the only problem I have is it only displays one of the letters missing.
Also how do I change the message "True" to "This string is a pangram" as it looked like they were being returned.
Many thanks
|
|
|
public class PangramTest
{
static String domain = "abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args)
{
String[] testStrs =
{
"hello world",
"the five boxing wizards jump quickly",
"the quick brown fox jumps over a lazy dog"
};
for(int j = 0; j < testStrs.length; j++)
{
String result = "This string is " +
(isPangram(testStrs[j]) ? "" : "not ") +
"a pangram.";
System.out.println(result);
}
}
private static boolean isPangram(String s)
{
// run through domain and look for each letter in s
// if we find them all, return true
// if we miss one, return false
boolean result = true;
char[] chars = domain.toCharArray();
for(int j = 0; j < chars.length; j++)
{
char next = chars[j];
if(s.indexOf(next) == -1)
{
if(result)
{
System.out.print("couldn't find " + next + ", ");
result = false;
}
else
System.out.print(next + ", ");
}
}
if(!result)
System.out.println();
return result;
}
}
|
|
|
|
|
|
|
|
|
|