ICSE BlueJ String Handling

String Handling Questions for BlueJ

(Note: All programs codes are Tested in BlueJ)

1. Accept a name and print it in reverse form. (If name is AMIT, then output is TIMA)
class ques1
{
  public static void main(String args[])
  {
    String n = "AMIT";
    int l = n.length();
    for(int i = l-1;i> = 0;i--)
    {
      System.out.print (n.charAt(i));
    }
  }
}
2. WAP to accept a name. Then display the ASCII value of each character present in that name.
class ques2
{
  public static void main(String args[])
  {
    String n = "AMITABH";
    int l = n.length();
    for(int i = 0;i<l;i++)
    {
      int asc = (int)n.charAt(i);
      System.out.println (n.charAt(i)+" --> "+asc);
    }
  }
}
3. WAP to read a word. Print the position of the first vowel occurring in the word. If there is no vowel in the word then print 'Sorry no vowel'.
class ques3
{
  public static void main(String args[])
  {
    String n = "AMITABH";
    int l = n.length();
    int p = 0;
    for(int i = 0;i<l;i++)
    {
      char c = n.charAt(i);
      if(c =  = 'A' || c =  = 'E' || c =  = 'I' || c =  = 'O' || c =  = 'U')
      {
        p = i+1;
        break;
      }
    }
    if(p =  = 0)
      System.out.println ("Vowel not found");
    else
      System.out.println ("Position of First Vowel "+p);
  }
}
4. WAP in BASIC to take a proverbial statement (like 'Failure are the pillars of success') as an input from the user and then output the frequency of the vowels occurring in the statement.
class ques4
{
  public static void main(String args[])
  {
    String n = "Failure are the pillars of success";
    int va = 0,ve = 0,vi = 0,vo = 0,vu = 0;
    for(int i = 0;i<n.length();i++)
    {
      char c = n.charAt(i);
      if(c =  = 'A' || c =  = 'a')       va++;
      if(c =  = 'E' || c =  = 'e')       ve++;
      if(c =  = 'I' || c =  = 'i')        vi++;
      if(c =  = 'O' || c =  = 'o')      vo++;
      if(c =  = 'U' || c =  = 'u')       vu++;
    }
    System.out.println ("No. of times Vowel 'A' is present : "+va);
    System.out.println ("No. of times Vowel 'E' is present : "+ve);
    System.out.println ("No. of times Vowel 'I' is present : "+vi);
    System.out.println ("No. of times Vowel 'O' is present : "+vo);
    System.out.println ("No. of times Vowel 'U' is present : "+vu);
  }
}
5. WAP to enter any sentence and calculate the following:
  a) Total number of digits present in it.
  b) Total number of small letters and capital letters present in it.
  c) Total number of alphabets used in it.
  d) Total number of special character used in it.
  e) Total number of vowels presents in it.
  f) Total Number words present in that sentence.
class ques5
{
  public static void main(String args[])
  {
    String n = "O-16, Shiva Ganga Apartment, Sonari, Phone No. 3317430";
    String alp = "ABCDEFGH IJKLMNOPQR STUVWXYZ";
    int nd = 0,sl = 0,cl = 0,alpha = 0,nv = 0,ns = 0;
    int l = n.length();
    for(int i = 0;i<l;i++)
    {
      char c = n.charAt(i);
      int asc = (int)c;
      if(asc> = 48 && asc< = 57)
        nd++;
      if(asc> = 65 && asc< = 90)
        cl++;
      if(asc> = 97 && asc< = 122)
        sl++;
      if(c =  = ' ')
        ns++;
      if(c =  = 'A'||c =  = 'a'|| c =  = 'E'||c =  = 'e'||c =  = 'I'|| c =  = 'i'||c =  = 'O'|| c =  = 'o'||c =  = 'U'||c =  = 'u')
        nv++;
      // calculating number of alphabets used
      if(Character.isLowerCase(c))
        c = Character.toUpperCase (c);
      int pos = alp.indexOf(c);
      if(pos! = -1)
      {
        alp = alp.replace(c,'0');
        alpha++;
      }
    }
    System.out.println ("Sentence is : "+n);
    System.out.println ("No. of Digits (0-9) present : "+nd);
    System.out.println ("No. of Small Letter : "+sl+" Capital Letter : "+cl);
    System.out.println ("No. of Alphabet Used : "+alpha);
    System.out.println ("No. of Special Character : "+(l-(nd+sl+cl+ns)));
    System.out.println ("No. of Vowels present : "+nv);
    System.out.println ("No. of Words present : "+(ns+1));
  }
}
6. Write programs to accept any word and check whether the word is palindrome or not.
class ques6
{
  public static void main(String args[])
  {
    String ow = "MALAYALAM";
    String rw = "";
    int l = ow.length();
    for(int i = l-1;i> = 0;i--)
      rw = rw+ow.charAt(i);
    if(ow.compareTo(rw) =  = 0)
      System.out.print ("Word is Palindrom");
    else
      System.out.print ("Word is not Palindrom");
  }
}
7. WAP to accept a name (in first name & last name format), then display that name in short format. (Example - SACHIN TENDULKAR becomes S. TENDULKAR)
class ques7
{
  public static void main(String args[])
  {
    String n = "SACHIN TENDULKAR";
    System.out.print (n.charAt(0)+".");
    int p = n.indexOf(' ');
    System.out.print (n.substring(p));
  }
}
8. WAP to accept a person's first, middle and last names in a string variable and then display in short form. (Example - ARUP KUMAR DAS becomes A. K. DAS)
class ques8
{
  public static void main(String args[])
  {
    String name = "ARUP KUMAR DAS";
    System.out.print (name.charAt(0)+". ");
    int firstSpace = name.indexOf(' ');
    firstSpace++;
    System.out.print (name.charAt(firstSpace)+".");
    int secondSpace = name.indexOf(' ',firstSpace);
    System.out.print (name.substring(secondSpace));
  }
}
9. WAP to converting all capital letters of a string into small letters (without using library function). For example: Input: MY NAME IS SUMIT Output: my name is sumit
class ques9
{
  public static void main(String args[])
  {
    String n = "MY NAME IS SUMIT";
    String u = "";
    for(int i = 0;i<n.length();i++)
    {
      char c = n.charAt(i);
      if(c> = 65 && c< = 90)  c = (char)(c+32);
      u = u+c;
    }
    System.out.println ("The Lower Case sentence is : "+u);
  }
}
10. WAP to accept any name (full name) and convert it to Proper Case.
class ques10
{
  public static void main(String args[])
  {
    String n = "ARUP KUMAR DAS";
    String properCase = "";
    n = n.toLowerCase();
    properCase  =  properCase + Character.toUpperCase (n.charAt(0));
    for(int i = 1;i<n.length();i++)
    {
      if(n.charAt(i) =  = ' ')
        properCase = properCase + " " + Character.toUpperCase (n.charAt (++i));
      else
        properCase = properCase+n.charAt(i);
    }
    System.out.print ("Proper Case Name is :"+properCase);
  }
}
11. WAP to accept any string and then convert each 'A' to 'AN' present in that string. Then print the string. (Note: remember 'A' should not be a part of a word?).
class ques11
{
  public static void main(String args[])
  {
    String n = "I AM A GOOD BOY";
    String s = "";
    n = n+" ";
    int p = 0,x;
    while(p<n.length())
    {
      x = n.indexOf(' ',p);
      String word = n.substring(p,x);
      if(word.compareTo("A") =  = 0)
        s = s+" "+"AN";
      else
        s = s+" "+word;
      p = x+1;
    }
    System.out.println ("The Converted sentence is : "+s);
  }
}
12. Sankalp has a terrible habit of deleting the last two letters of a word beginning with 'E' and adding a single letter 'O' in their place. WAP to convert a given word Exterminate or Ellipse according to Sankalp habit.
class ques12
{
  public static void main(String args[])
  {
    String w = "EXTERMINATE";
    int l = w.length();
    if(w.charAt(0) =  = 'E' || w.charAt(0) =  = 'e')
      System.out.println ("Word is : "+w.substring(0,l-2)+"O");
    else
      System.out.println ("Word is unchanged : "+w);
  }
}
13. In Piglatin a word such as KING becomes INGKAY, TROUBLE becomes OUBLETRAY as so on. The first vowel of the original word becomes the starting of the translation and proceeding letter being shifted towards the end and followed by AY. Word that begins with a vowel is left unchanged. WAP to accept a word and convert in to Piglatin word.
class ques13
{
  public static void main(String args[])
  {
    String n = "TROUBLE";
    int p = 0,i;
    for(i = 0;i<n.length();i++)
    {
      char c = n.charAt(i);
      if(c =  = 'A' || c =  = 'E' || c =  = 'I' || c =  = 'O' || c =  = 'U')   break;
    }
    if(i =  = 0)       System.out.println (n);
    else       System.out.println ("Piglatin word " + n.substring(i) + n.substring(0,i) + "AY");
  }
}
14. Accept two strings, a word and a sentence. Then find number of times the word is present in given string. If I enter 'THE' and 'THE BIG FAT THE ODORE', then the computer should display 2.
class ques14
{
  public static void main(String args[])
  {
    String n = "THE BIG FAT THE ODORE";
    n = n+" ";
    String w = "THE";
    int p = 0,x,ctr = 0;
    while(p<n.length())
    {
      x = n.indexOf(' ',p);
      String word = n.substring(p,x);
      if(w.compareTo(word) =  = 0)
        ctr++;
      p = x+1;
    }
    System.out.println ("No. of times word "+w+" present in sentence is : "+ctr);
  }
15. WAP to accept a sentence then convert each character to second next character. The character A becomes C, Y becomes A and Z becomes B.
class ques15
{
  public static void main(String args[])
  {
    String n = "WE R GOOD BOYZ";
    for(int i = 0;i<n.length();i++)
    {
      int asc = (int)n.charAt(i);
      if(asc>65 && asc< = 88)
        asc+ = 2;
      else
        if(asc =  = 89)
          asc = 65;
        else
          if(asc =  = 90)
            asc = 66;
      System.out.print ((char)asc);
    }
  }
}
16. A name is to be said as odd name if the ASCII code of each character become an odd number. Write a program to accept a name and check whether the given name is odd name or not.
class ques16
{
  public static void main(String args[])
  {
    String n = "MOUSUMI";
    int flag = 0;
    for(int i = 0;i<n.length();i++)
    {
      int asc = (int)n.charAt(i);
      if(asc%2 =  = 0) flag = 1;
    }
    if(flag =  = 0)
      System.out.println (n+" is a odd Name");
    else
      System.out.println (n+" is not a odd Name");
  }
}
17. Write a program for converting a sentence in reverse. (Input: BASIC IS PROGRAMMING     Output: PROGRAMMING IS BASIC)
class ques17
{
  public static void main(String args[])
  {
    String n = "BlueJ is Programming";
    n = n+" ";
    String rs = "";
    int p = 0,x;
    while(p<n.length())
    {
      x = n.indexOf(' ',p);
      String w = n.substring(p,x);
      rs = w+" "+rs;
      p = x+1;
    }
    System.out.println ("The Sentence is : "+rs);
  }
}
18. Write a program to accept a binary number and convert it to decimal number. Also convert each binary digit to its compliment and then convert it to decimal number.
class ques18
{
  public static void main(String args[])
  {
    String b = "1010111";
    String cb = "";
    int tp = 1,dec = 0,cdec = 0;
    int l = b.length();
    for(int i = l-1;i> = 0;i--)
    {
      // calculating decimal value of binary number and compliment binary number
      if(b.charAt(i) =  = '1')
      {
        dec = dec+(1*tp);
        cdec = cdec+(0*tp);
      }
      else
      {
        dec = dec+(0*tp);
        cdec = cdec+(1*tp);
      }
      tp* = 2;
      // find out the compliment of binary number
      if(b.charAt(i) =  = '1')
        cb = "0"+cb;
      else
        cb = "1"+cb;
    }
    System.out.println ("Binary Number : "+b+" Decimal Value : "+dec);
    System.out.println ("Compliment of Binary number : "+cb+" Decimal Value : "+cdec);
  }