• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

To Perform various String Operation in Java

July 1, 2012 by ProjectsGeek Leave a Comment

Assignment No 01

 

 AIM:

To perform String operations using java such as calculating length of string, lowercase and uppercase conversions, concatenation of strings, reverse the string etc. Accept the String from user.

THEORY:

 

Features Of Java

 

Java is an object-oriented, multi-threaded programming language developed by Sun Microsystems in 1991. Java Development Environment consists of Java complier and a Java interpreter. Java complier generates byte code instead of machine code and Java interpreter executes the Java program. The disadvantage of using byte code is the execution speed. In order to write Java program, an editor, a java complier and a java runtime Environment are needed.

 

The important features of java are

 

  • Simple and powerful
  • Secure and Portable
  • Robust
  • Multithreaded
  • Distributed and dynamic
  • Object-oriented

String Operations

 

A combination of characters is a string. Strings are instances of the class String. The addition operator(+) can be used to create and concate the strings.

 

String names[ ] ={“anand”, ”Rahul”, ”sachin”};

 

Calling the default constructor with no parameters can create an empty string.

 

String s= new String( );

 

In order to create a string initialized with characters, we need to pass an array of char to the constructor.

 

char chars [ ] ={‘a’, ’b’, ’c’);

 

String s= new String (chars);

 

System.out.println(s);

 

Some String methods are :

Method

 

Use

 

s.Length( )

 

Length of the string

 

s.charAt( index)

 

Character at a location given by index

 

s.equals(s1), s.equalsIgnoreCase(s1 )

 

An equality checking for two strings

 

s.compareTo(s1)

 

Result is negative, positive or zero depending on the lexicographical ordering

 

s.indexOf(char c), s.lastIndexOf(char c )

 

Returns the index or –1 if the character not found.

 

s.substring( )

 

Returns a new string object containing the required character set.

 

s.concat( )

 

Returns a new string object  with the concatenation done.

 

s.replace( )

 

Returns a new string object  with the replacement done.

 

s.toLowercase( ), s.toUpperCase( )

 

Returns a new string object with the

 

Lowercase case or uppercase change.

 

s.trim( )

 

Returns a new string object  with the white spaces removed from each end.

 

Strings represent fixed length character sequences and StringBuffer represents variable length character sequences.

 

StringBuffer sb=new StringBuffer(“abc”);

 

sb.append (“xyz”);

 

//the argument gets appended to the end of the current StringBuffer.

 

Casting is used to convert the value of an object or primitive type into another type. Conversion of primitive types cannot be done explicitly.

 

CONCLUSION

 

 

Various String operations such as calculating the length of the string, reverse the string, palindrome, concatenation of the string, finding the substring are successfully created.

 

 

PROGRAM

 

import java.io.*; 
public class Main {

    public Main() {
    }
    public static void main(String[] args) throws Exception{
        BufferedReader ss=new BufferedReader (new InputStreamReader(System.in));
        System.out.println("enter the desired string:\t");
        String str=ss.readLine(); 
        BufferedReader b= new BufferedReader(new InputStreamReader (System.in));
        int choice,temp;
        String c1,c2;
        do{
          System.out.println("select desired option:\n1.length \n2.character At.\n3.equality\n4.compare\n5.index of char.\n6.last index of char.\n7.substring\n8.concat\n9.replace\n10.to lowercase\n11.trim \n12. Reverse");
          choice=Integer.parseInt (b.readLine());
        switch(choice)
        {
            case 1:
                 System.out.println("String length is "+str.length()+"\n\n");
                 break;
            case 2:
                System.out.println("select index to determine character: ");
                temp=Integer.parseInt (b.readLine());
                System.out.println("CHARACTER AT INDEX " +temp+ " is "+ str.charAt(temp-1));
                break;
            case 3:
                System.out.println("ENTER SECOND STRING: ");
                String str2= ss.readLine();
                System.out.println("the condition that strings "+str+" and "+ str2+" are equal is: "+ str.equalsIgnoreCase(str2));
                break;
            case 4:
                System.out.println("ENTER SECOND STRING: ");
                str2= ss.readLine();
                temp=str.compareTo(str2);
                if(temp==0)
                    System.out.println("THE STRINGS ARE EQUAL ");
                else if(temp>0)
                    System.out.println("THE STRING "+str+" is greater than the string "+str2);
                else
                    System.out.println("THE STRING "+str2+" is greater than the string "+str);
                break;
            case 5:
                System.out.println("ENTER CHAR: ");
                str2=ss.readLine();
                System.out.println(str.indexOf(str2));
                break;
            case 6:
                System.out.println("ENTER CHAR: ");
                str2=ss.readLine();
                System.out.println(str.lastIndexOf(str2));
                break;
            case 7:
                System.out.println("ENTER SUBSTRINGs STARTING AND ENDING INDEX : ");
                int start=Integer.parseInt(ss.readLine());
                int end=Integer.parseInt(ss.readLine());
                System.out.println(str.substring(start,end));
                break;
            case 8:
                System.out.println("ENTER SECOND STRING: ");
                str2= ss.readLine();
                System.out.println(str.concat(str2));
                break;
            case 9:
                System.out.println("ENTER CHAR TO BE REPLACED: ");
                c1= ss.readLine();
                System.out.println("ENTER CHAR TO REPLACE: ");
                c2= ss.readLine();
                System.out.println(str.replace(c1,c2));
                break;
            case 10:
                System.out.println(str.toLowerCase());
                break;
            case 11:
                System.out.println(str.trim());
                break;
            case 12:
        StringBuffer sb = new StringBuffer(str);
        System.out.println(sb.reverse());
break;        
            default:
System.out.println(“Invalid Choice”);
        }
        }while(choice!=12);
    } 
}

 

To Perform various String Operation in Java 3
Download Code Here
 
 

Other Projects to Try:

  1. string operations such as Copy, Length, Reversing, Palindrome, Concatenation
  2. String Operations in C Program
  3. Implement using Socket Programming (TCP/UDP) in Java
  4. How to append two strings in PHP | Simple ways
  5. To Perform File Handling in Java

Filed Under: Software Development Tools Lab

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy