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

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 ),...
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...
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.
(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 } }
I wrote this program to check a string is palindrome or not. but in both cases,...
I wrote this program to check a string is palindrome or not. but in both cases, it gives me palindrome. could someone help me with this program? it has an error I couldn't find. when I run the code and give a string, in both cases it gives me palindrome. for example if I give Pop it says it is a palindrome but if I give Salima, it also says palindrome. #include<string> #include <iostream> using namespace std; class PString:public string...
Can someone please tell me on how can I have a double and character as one...
Can someone please tell me on how can I have a double and character as one of the items on my list? Thanks! This is what I got so far and the values I am getting are all integers. #pragma once #include <iostream> class Node { public:    int data;    Node* next;       // self-referential    Node()    {        data = 0;        next = nullptr;    }    Node(int value)    {        data...
c++ I want to flip the string when it's string type. For example, if it is...
c++ I want to flip the string when it's string type. For example, if it is 'apple', I would like to print 'elppa'. how can i do?
c++ How do I get it before a particular string when I use <ifstream>? For example,...
c++ How do I get it before a particular string when I use <ifstream>? For example, when it's 'Apple: fruit', I only want to get the previous ':' text (Apple). And then I want to move on to the next row. How can i do?
Create a method that returns the third character from the String word. Be sure to use...
Create a method that returns the third character from the String word. Be sure to use a try catch block and use the appropriate exception to deal with indexes out of a String’s bound: StringIndexOutOfBoundsException. Return the character 'x' (lowercase) when this exception is caught. Do not use if statements and do not use the general exception handler Exception. Examples: thirdLetter("test") -> 's' public char thirdLetter(String word) { } ​should return the char 'r'
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT