In: Computer Science
How can I check that if a string only make from
specific character?
For example, I want to check if a string only make from ICDM
characters. It pass the test if it makes from ICMD. It fail the
test if it makes from any special character like @#$& or lower
case.
Example inputs:
if string = CIM, it passed
if string = AIM, it failed
if string = MCDI, it passed
if string = ICDF, it failed
This can be in either java or php language. Any help
with detail is appreciated. Thank you!
public class StringTest {
public static void main(String[] args) {
String arr[] = { "CIM", "AIM",
"MCDI", "ICDF" };
String str="ICDM";
boolean passed=true;
// iterating the each word
for(String s:arr ){
passed=true;
//iterating the
each char in the wor
for(int
i=0;i<s.length();i++){
//checking if is exist in the original
word
if(!str.contains(s.charAt(i)+"")){
passed=false;
break;
}
}
System.out.print(s+" : ");
if(passed)
System.out.println("Passed");
else
System.out.println("Failed");
}
}
}