String Handling Test Questions for Java/BlueJ

(Note: All programs codes are Tested in BlueJ)

[Set I]
Time: 1 Hrs                                                                            Marks: 4x5=20
1. Write a program to accept name (first, middle and last name format) of a person (given by the user) in a string variable and then display in short form. (Example - SACHIN RAMESH TENDULKAR becomes S. R. TENDULKAR)
class test1q1
{
  public static void main(String name)
  {
    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));
  }
}
2. Write a program to accept a sentence then convert it to Title Case. (e.g. MY NAME IS AMITABH become My Name Is Amitabh)
class test1q2
{
  public static void main(String n)
  {
    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("Title Case is :" + properCase);
  }
}
3. In Piglatin a word such as KING becomes INGKAY, trOUBLE becomes OUBLEtrAY. 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. Write a program to convert a word (given by the user) to Piglatin word.
class test1q3
{
  public static void main(String n)
  {
    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");
  }
}
4. Write a program to accept any word and check whether the word is palindrome or not. (Without storing the reverse word in to another variable).
class test1q4
{
  public static void main(String w)
  {
    int l=w.length();
    int i,j,flag=0;
    for(i=0,j=l-1;i<l;i++,j--)
    {
      if(w.charAt(i)!=w.charAt(j))
        flag=1;
    }
    if(flag==0)
      System.out.print ("Word is Palindrom");
    else
      System.out.print ("Word is not Palindrom");
  }
}
[Set II]
Time: 1 Hrs                                                                            Marks: 4x5=20
1. Write a program to enter any sentence and calculate total number of alphabets used in it.
class test2q1
{
  public static void main(String n)
  {
    n=n.toUpperCase();
    String alp="ABCDEF GHIJKLMNOPQRS TUVWXYZ";
    int pos,ctr=0;
    for(int i=0;i<n.length();i++)
    {
      char c=n.charAt(i);
      pos=alp.indexOf(c);
      if(pos!=-1)
      {
        alp=alp.replace(c,'0');
        ctr++;
      }
    }
    System.out.println ("The Number of Alphabets used in the Given Sentence is : " + ctr);
  }
}
2. Write a program to accept any name and convert it to Proper Case. E.g. Input: AMITABH BANERJEE Output: Amitabh Banerjee
class test2q2
{
  public static void main(String n)
  {
    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("Name in Proper Case is : " + properCase);
  }
}
3. Write a program to accept a word and then find how many times two successive alphabet present in that word. e.g. Input: AMITABH Output: 1
class test2q3
{
  public static void main(String word)
  {
    int length=word.length(), ctr=0;
    for(int i=0;i<length-1;i++)
    {
      char firstChar = word.charAt(i);
      char nextChar = word.charAt(i+1);
      if(firstChar+1==nextChar)
        ctr++;
    }
    System.out.println ("Number of two successive letter : "+ctr);
  }
}
4. Write a program to accept a word then convert each character to the same character position from last. For example if the character is A then it becomes to last character Z, if the character is B it becomes to Y and so on. E.g. Input: BASIC Output: YZHRX)
class test2q4
{
  public static void main(String n)
  {
    n=n.toUpperCase();
    for(int i=0;i<n.length();i++)
    {
      int asc=n.charAt(i);
      int oppasc=90-(asc-65);
      System.out.print ((char)oppasc);
    }
  }
}
5. Write a program to accept the name of a person in first, middle and last name format in a single variable. Then display the name in the following format, Input: MOHANDAS KARAMCHAND GANDHI Output: GANDHI M. K.
class test2q5
{
  public static void main(String name)
  {
    String newName="";
    newName = newName + name.charAt(0)+". ";
    int firstSpace=name.indexOf(' ');
    firstSpace++;
    newName = newName + name.charAt(firstSpace)+".";
    int secondSpace = name.indexOf(' ',firstSpace);
    newName = name.substring(secondSpace) + " " + newName;
    System.out.print ("Name is : " + newName);
  }
}
[Set III]
Time: 1 Hrs                                                                            Marks: 4x5=20
1. Write a program to convert a given sentence in reverse. E.g. Input: BASIC IS PROGRAMMING Output: PROGRAMMING IS BASIC)
class test3q1
{
  public static void main(String n)
  {
    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);
  }
}
2. Write a program to accept a sentence then convert each character to second next character. The character A becomes C, Y becomes A & Z becomes B.
class test3q2
{
  public static void main(String n)
  {
    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);
    }
  }
}
3. Write a program that will accept a sentence and a word. Then delete that word from the given sentence, if the word is present otherwise display an error message. E.g. if user enter a sentence "I CAN NOT DO IT" and a word "NOT", then the output will be "I CAN DO IT".
class test3q3
{
  public static void main(String n,String w)
  {
    n=n+" ";
    String newWord="";
    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)
        newWord = newWord + " " + word;
      p=x+1;
    }
    System.out.println ("New Sentence is " + newWord);
  }
}
4. A word is called "Word Square", if it is formed by joining same word. E.g. TINTIN, TATA, MOONMOON. Write a program to accept a word and check whether the word is "Word Square" or not.
class test3q4
{
  public static void main(String word)
  {
    String word1="",word2="";
    int l=word.length(),h=l/2,i;
    for(i=0;i<h;i++)
    {
      word1=word1 + word.charAt(i);
      word2=word2 + word.charAt(i+h);
    }
    if(word1.compareTo(word2) == 0)
      System.out.print ("Word Square");
    else
      System.out.print ("not Word Square");
  }
}
5. Write a program to accept any word and check whether the word is palindrome or not. (Without storing the reverse word in to another variable).
class test3q5
{
  public static void main(String w)
  {
    int l=w.length();
    int i,j,flag=0;
    for(i=0,j=l-1;i<l;i++,j--)
    {
      if(w.charAt(i)!=w.charAt(j))
        flag=1;
    }
    if(flag==0)
      System.out.print ("Word is Palindrom");
    else
      System.out.print ("Word is not Palindrom");
  }
}