Sunday, January 17, 2010

BlueJ Programs Using String Functions

In my previous post, I have discussed about the basic String class functions. Today’s post is again on String class functions :

1. Function prototype is int compareTo(String str1) - this function is normally used for sorting purpose. This function is invoked on one string and takes another string as argument. If both are same, the function returns numeric 0. If the invoking string is greater than the argument string, it returns greater than 0 and it returns less than 0 for the reverse. Actually for unequal string the function return the ASCII difference of the invoking string character and the argument string character. This is a case sensitive function.

2. Function prototype is int compareToIgnoreCase(String str1) - Same as the above excepting this function is not case sensitive. So “BURDWAN”. compareToIgnoreCase (“burdwan”) will return 0.

3. int indexOf () – This function is used to search a string or character from the invoking string. If the search is successful it returns the first occurrence location of the sub string or character within the invoking string otherwise returns -1. This function has several overloaded versions.

Function prototype : int indexOf (String str)- it returns the first occurrence location of the sub string ‘str’ within the invoking string.

Function prototype : int indexOf (char ch)- it returns the first occurrence location of the character ‘ch’ within the invoking string.

Function prototype :int indexOf (String str, int index)- it returns the first occurrence location of the sub string ‘str’ after the specified location ‘index’ within the invoking string.

Function prototype : int indexOf (char ch, int index)- it returns the first occurrence location of the character ‘ch’ after the specified location ‘index’ within the invoking string.

String substring () is a function of String class which can return a portion of the invoking string. This function has two overloaded versions.

Function prototype :String substring(int index) - this function returns the sub string of the invoking string starting from the location ‘index’ to end of the string.

Function prototype :String substring(int start, int end) - this String class function returns the sub string of the invoking string starting from the location ‘start’ to the location ‘end’ of the string.

5. We can concatenate two strings using String concat () function.
Function prototype :is String concat(String str1);
This function takes one string as argument and is invoked on another string, resulting a new string where the argument string is appended to the end of the invoking string

String str=”This is ”;
String str1=str.concat(“Burdwan”);
System. out.println(str1);
Output will be : This is Burdwan

String replace () is another function of String class which is used to replace any specified character of the invoking string with another character.
Function prototype : String replace(char ch1, char ch2);
This function takes two characters as argument. The first character is replaced with the second character of the invoking string and will generate a new string object.

String s1=”Hello”;
String s=s1.replace(‘l’,’b’);
Now the value of s would be Hebbo

7.String trim () is used to remove any leading or trailing spaces from the invoking string object.

String str=” ChakraInfotech “.trim();
Now the value of ‘str’ will be: ChakraInfotech

8. To change the case of characters in a string we can use the function String toUpperCase () and String toLowerCase ().

Function prototype :String toUpperCase () - converts all the lower case characters in a string to upper case and the upper case characters will remain unchanged.

Function prototype : String toLowerCase () is just the opposite of the above function.

This page is on String functions. If you have any doubt in any program, pl. feel free to put your comments. I am here to clear your doubts.

Related Post:  BlueJ Programs on String/Sentence

16 comments:

  1. can u plzzzz explain string trim....

    ReplyDelete
  2. Prototype of this trim() function is : String trim(); This function when invoked on a string object, removes the leading and trailing spaces from the invoking string and returns a new string object with the modified value.
    e.g String str=" This is a string example ";
    String str1=str.trim();
    After calling the function value of str1 would be
    "This is a string example"; Extra space between the words 'string' and 'example' will not be removed.

    ReplyDelete
  3. How can we write a program in java so that we input a string and display it in toggle case
    Sample input-Computer Is Fun.
    Sample output-cOMPUTER iS fUN

    ReplyDelete
  4. Please check the post on 12 January 2011.

    ReplyDelete
  5. can u provide me with a small program converting a lower case string into an upper case 1...

    ReplyDelete
  6. import java.io.*;

    class Word
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str1,str2="";
    int i,j;
    char ch;
    public void take() throws Exception
    {
    System.out.println("Enter the sentence:");
    str1=br.readLine().trim();
    j=str1.length();
    for(i=0;i< j;i++)
    {
    ch=str1.charAt(i);
    if(ch>=97 && ch<=122)
    ch=(char)(ch-32);
    str2=str2+ch;
    }
    str2=str2.trim();

    System.out.println(str2+ "is the modified string.");

    }

    public static void main(String args[]) throws Exception
    {
    Word ob=new Word();
    ob.take();
    }
    }

    ReplyDelete
  7. hello sir,how to covert the first letter of a sentence into capital letter??
    eg: bluej is the best..
    i want the output as this...
    output--Bluej Is The Best..

    plz help me sir..!!!

    ReplyDelete
  8. import java.io.*;
    class Binary
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String str;
    int i,len;
    char ch;
    public void show() throws Exception
    {
    System.out.println("Enter the sentence:");
    str=br.readLine().trim();
    len=str.length();
    ch=str.charAt(0);
    if(ch>=97 && ch<=122)
    ch=(char)(ch-32);
    System.out.print(ch);
    for(i=1;i< len;i++)
    {
    if(str.charAt(i)==' ')
    {
    System.out.print(str.charAt(i));
    ch=str.charAt(i+1);
    if(ch>=97 && ch< =122)
    ch=(char)(ch-32);
    System.out.print(ch);
    i++;
    }
    else
    System.out.print(str.charAt(i));
    }
    }
    public static void main(String args[]) throws Exception
    {
    Binary ob=new Binary ();
    ob.show();
    }
    }

    ReplyDelete
  9. can you give a program to replace all the vowels in a string with the next vowel?
    Example:
    Input:"A Dog is an useful animal."
    Output:"E Dug os en asifal enomel."

    ReplyDelete
  10. Check my post on 11 September 2011.

    ReplyDelete
  11. I want a program to reverse a string.
    Example:
    Input:I like computer applications
    Output:I ekil retupmoc snoitacilppa.

    please help.

    ReplyDelete
  12. Here is the program

    import java.io.*;
    class Convert
    {
    String str1,str2;
    int i,j,len;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    public void read() throws IOException
    {
    System.out.println("Enter the sentence:");
    str1=br.readLine();
    while(true)
    {
    i=str1.indexOf(' ');
    if(i< 0)
    break;
    str2=str1.substring(0,i);
    str1=str1.substring(i+1);
    len=str2.length();
    for(j=len-1;j >=0;j--)
    System.out.print(str2.charAt(j));
    System.out.print(" ");
    }
    len=str1.length();
    for(j=len-1;j >=0;j--)
    System.out.print(str1.charAt(j));


    }

    public static void main(String args[]) throws IOException
    {
    Convert ob=new Convert();
    ob.read();
    }
    }

    ReplyDelete
  13. sir can u help me do this pattern
    HAPPY-HAPPY
    HAPP---APPY
    HAP-----PPY
    HA-------PY
    H---------Y
    HA-------PY
    HAP-----PPY
    HAPP---APPY
    HAPPY-HAPPY
    FOR THE FIRST AND THE LAST STATEMENT THE(-)IS NOT SPACE BUT IS A PART OF THE STRING BUT FOR THE OTHERS IT REPRESENTS SPACE
    PLEASE IF U COULD HELP ME!!!!!!

    ReplyDelete
    Replies
    1. class Enen
      {
      int i,j,k,len,x=-2,a;
      public void show(String s)
      {
      len=s.length();
      for(i=0;i<len*2-1;i++)
      {
      if(i<len)
      a=i;
      else
      a--;
      for(j=0;j<len-a;j++)
      System.out.print(s.charAt(j));
      if(i<len)
      x=x+2;
      else
      x=x-2;

      for(k=0;k<=x;k++)
      {
      if(i==0 || i==len*2-2)
      System.out.print("-");
      else
      System.out.print(" ");
      }

      for(j=a;j<len;j++)
      System.out.print(s.charAt(j));
      System.out.println();
      }
      }
      public static void main(String args[])throws Exception
      {
      Enen ob=new Enen();
      ob.show("HAPPY");
      }
      }

      Delete
  14. please help to solve this program.
    input a sentence and print it after deleting all appearance of the alphabet 'a'.
    e.g
    input:"India is awesome"
    output: "Indi is wesome".

    ReplyDelete
    Replies
    1. Please check today's post: 16 October 2014.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner