logo codesdope
Level 1
1.
Write a program to print a string entered by user.
Answer
import java.util.*;

class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    String a = s.next();
    System.out.println(a);
  }
}									

2.
Write a program to input and display the sentence I love candies.

3.
Write a program to find the length of the string "refrigerator".
Answer
class Ans{
  public static void main(String[] args){
    String a = "refrigerator";
    System.out.println(a.length());
  }
}									

4.
Write a program to check if the letter 'e' is present in the word 'Umbrella'.
Answer
class Ans{
  public static void main(String[] args){
    String a = "Umbrella";
    boolean per = false;
    for(int i = 0;i<a.length();i++){
      if(a.charAt(i) == 'e'){
        per=true;
        break;
      }
    }
    System.out.println(per);
  }
}								

5.
Write a program to check if the word 'orange' is present in the "This is orange juice".

6.
Write a program to find the first and the last occurence of the letter 'o' and character ',' in "Hello, World".
Hint
For first occurance, iterate from beginning and for last ocurrance, iterate from last.

7.
Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R.B.Roser.
Answer
import java.util.*;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    String st = s.nextLine();
    String sr = "";
    sr = sr+st.charAt(0);
    sr = sr+". ";
    for (int i = 0; i<st.length();i++){
      if(st.charAt(i) == ' ' && st.charAt(i+1)!=' ' && i+1<st.length()){
        sr = (sr+st.charAt(i+1)).toUpperCase();
        sr = sr+". ";
      }
    }
    String last_wrd = "";
    //to get the last word
    for(int i = st.length()-1;i>=0;i--){
      if(st.charAt(i) == ' '){
        last_wrd = st.substring(i+2);
        break;
      }
    }
    //to remove last ". "
    sr = sr.substring(0,sr.length()-2);
    sr = sr+last_wrd;
    System.out.println(sr);
  }
}
								

8.
Write a program to find the number of vowels, consonents, digits and white space characters in a string.

9.
Write a program to delete all consonents from the string "Hello, have a good day".

10.
Input a string of alphabets. Find out the number of occurrence of all alphabets in that string. Find out the alphabet with maximum occurrence.
Hint
You can use an array of length 26 to store the number of occurance of each character in the elements of the array.
Level 2
1.
Write a program to check if a given string is a Palindrome.
A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc.
Answer
import java.util.*;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    String st = s.nextLine();
    boolean pali = true;

    for(int i = 0;i<(st.length()/2);i++){
      if(st.charAt(i)!=st.charAt(st.length()-1-i)){
        pali = false;
        break;
      }
    }
    System.out.println(pali);
  }
}
								

2.
Write a program to find out the largest and smallest word in the string "This is an umbrella".

3.
Write down the names of 10 of your friends in an array and then sort those in alphabetically ascending order.
Answer
import java.util.*;
class Ans{
  public static void main(String[] args){
    String[] a = {"Jack","Jhon","Aman","Brute","Hank","Canny","Lee","Louis","June","Frank"};

    for(int i = 0;i<a.length-1;i++){
      for(int j = i;j<a.length;j++){
        if(a[i].compareTo(a[j])>1){
          String temp;
          temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }

    for(String i:a){
      System.out.println(i);
    }
  }
}
								

4.
Write a program to check if the two strings entered by user are anagrams or not. Two words are said to be anagrams if the letters of one word can be rearranged to form the other word. For example, jaxa and ajax are anagrams of each other.

5.
Input a string which contains some palindrome substrings. Find out the position of palindrome substrings if exist and replace it by *. (For example if input string is “bob has a radar plane” then it should convert in “*** has a ***** plane”.
Answer
import java.util.*;
class Ans{

  public static boolean isPali(String st){
    boolean pali = true;

    for(int i = 0;i<(st.length()/2);i++){
      if(st.charAt(i)!=st.charAt(st.length()-1-i)){
        pali = false;
        break;
      }
    }

    return pali;
  }

  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    String a = s.nextLine();

    int last_index = 0;
    String replace = "";

    for(int i = 0;i<a.length();i++){
      if(a.charAt(i) == ' '){
        if(isPali(a.substring(last_index,i))){
          for(int j = last_index;j<=i-1;j++){
            replace = replace+"*";
          }
          last_index = i+1;
          replace = replace+" ";
        }
        else{
          for(int j = last_index;j<=i-1;j++){
            replace = replace+a.charAt(j);
          }
          last_index = i+1;
          replace = replace+" ";
        }
      }
    }

    if(isPali(a.substring(last_index,a.length()))){
      for(int i = last_index;i<a.length();i++){
        replace = replace+"*";
      }
    }
    else{
      for(int i = last_index;i<a.length();i++){
        replace = replace+a.charAt(i);
      }
    }

    System.out.println(replace);
  }
}
								

6.
Write a program to replace a given substring in a sentence with another string. For example, in the sentence, ” A batman with bat” if we replace ”bat” with ”snow”, the new sentence should be printed as ”A snowman with snow”.

7.
Write a program to reverse individual words in a string, where each word may be delimited by a dot, comma, space or tab, like www.google.com should become www.elgoog.moc.
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.