Question

In: Computer Science

How can I check that if a string only make from specific character? For example, I...

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!

Solutions

Expert Solution


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


Related Solutions

how can I save a character stack to a string and then save that string into...
how can I save a character stack to a string and then save that string into a new string arraylist in java? so if the character stack is h, e, l, l, o i want it to save "hello" to a string and then put that string into an array list.
How can I create a hexadecimal number in c code from a specific array. For example...
How can I create a hexadecimal number in c code from a specific array. For example I have a[4]={2,5,7,4}; and I want to create this number : 0x004725 by adding 1 number at a time using << operation. Thank you!
Write a recursive function to check if a string whose each character is stored in a...
Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github. // // Doubly-linked list with 2 dummy nodes // #pragma once #include <stdexcept> template<typename T> struct Node { T data; Node<T>* next; Node<T>* prev; Node() = delete; // Intentionally no default constructor Node( const T & element ) : data( element ), next( nullptr ),...
Phython: Given a string x, write a program to check if its first character is the...
Phython: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
How can I make a method to check out a customer given their name and booking...
How can I make a method to check out a customer given their name and booking number in Java. I have provided the Book class as well as the customer class. I have done the method to add a customer into the array however im failng to implement a method that checks out the customer. If you need more classes associated with the system please let me know ASAP and I will provide them, ----------------------------------------------------------------------------------------------------------------------------------- public class Book{    String...
In Java, how can I convert a string user input for example a student starts a...
In Java, how can I convert a string user input for example a student starts a uni degree in "Winter/2022", to a date type, and then once I have done that, add 3 years to that value, and changing the season, so for example Student Started: Winter/2022 and Student Finished: Summer/2025. Is there any possible way to do this? Thank you.
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
how do I make a histogram. I am using the example from the book Essentials of...
how do I make a histogram. I am using the example from the book Essentials of Statistics Chapter 2.2, Problem 9BSC.
How to make a python program that imports only turtle and math library where I can...
How to make a python program that imports only turtle and math library where I can click once on the screen to set the center of the square, move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point?
(In java) Return a copy of the string with only its first character capitalized. You may...
(In java) Return a copy of the string with only its first character capitalized. You may find the Character.toUpperCase(char c) method helpful NOTE: Each beginning letter of each word should be capitalized. For example, if the user were to input: "United States of America " --> output should be "United States of America" NOT "United states of america" -------------------------------------- (This code is given) public class Class1 { public static String capitalize(String str) {     //add code here } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT