List of Java String programs

String is the collection of characters,but in java string is object which is immutable means (not changeable).

program 1:

Write a program to print the string in java.

public class FirstStringProgram
{
public static void main(String[] arg)
{
String name="suman kumar";
System.out.println("The name is given: "+name);
}
}

Output:

The name is given:suman kumar

program 2:

Write a program to find out the length of the string.

Suppose “suman” is string the length shoulde be 5.

public class StringLengthProgram
{
public static void main(String[] arg)
{
String name="suman";
int length=name.length();//Finding the length using the lenght() 
method.
System.out.println("The length of suman=" +length);
}
}

Output:

The length of suman=5

Program 3:

Write a program to find out the reverse of string.

Suppose string “suman” is given then output should be “namus”.

public class ReverseStringProgram
{
public static void main(String[] arg)
{
String rev="";
String name="suman";
for(int i=name.length()-1;;i>=0;i--)
{
rev=rev+charAt(i);//Here charAt is used for finding the array index
}
System.out.println("Reverse of string="+rev);
}
}

Output:

Reverse of string=namus

Program 4:

Write a program of palindrome of string

Suppose input is given “madam” and when reverse the “madam” then output should be “madam”.

public class PalindromeProgram
{
public static void main(String[] arg)
{
String name="madam";
String rev="";
String givenInput=name;
for(int i=name.lenght-1;i>=0;i--)
{
rev=rev+charAt(i);
}
if(givenInput.equals(rev)
{
System.out.println("String is palindrome");
}
else
{
System.out.println("String is not palindrome");
}
}
}

Output:

String is palindrome

Program 5:

Write a program to print a,b,c,d…..z using for loop.

public class AlphabetsProgram
{
public static void main(String[] arg)
{
Char ch;
for(ch='a';ch<=z;ch++)
{
System.out.println(ch);
}
}
}

Output:

a
b
c
.
.
.
z

Program 6:

Write a program to convert character into the string.

public class CharacterToStringProgram
{
public static void main(String[] arg)
{
Char ch='a';
String name=Character.toString(ch);
System.out.println("String will be="+name);
}
}

Output:

String will be=a

Note:

1.Here 'toString' is used to convet character into the string.
2.ValueOf() is also used to convert the character into the string. 

program 7:

Write a program to convert the string into the char array.

public class StringToChararrayProgram
{
public static void main(String[] arg)
{
String name="suman";
char []array=name.toCharArray();
System.out.print(Array.toString(array));
}
}

Output:

s,u,m,a,n

Note:

Here toCharArray( ) is used to convert string into the char array.

Program 8:

Write a program to convert char array into the string.

public class CharArrayToStringProgram
{
public static void main(String[] arg)
{
Char [] array={'s','u','m','a','n'};
String name=new String(array);
System.out.print(name);
}
}

Output:

suman

Program 9:

Write a program to concatenate two string.

public class StringConcatProgram
{
public static void main(String[] arg)
{
String s1="placement";
String s2="preparation";
String s3=s1.concat(s2);
System.out.print(Concatenation string will be="+s3);
}
} 

Output:

Concatenation string will be=placementpreparation

Program 10:

Write a program to convert the string into the lowercase &uppercase vice-versa.

public class LowerUpperCaseProgram
{
public static void main(String[] arg)
{
String name="suman kumar";
String name1="PLACEMENT PREPARATION";
System.out.println("The uppercase of string is given=" +name.toUpperCase());
System.out.println("The lowercase of string is given="+name1.toLowerCase());
}
}

Output:

The uppercase of string is given=SUMAN KUMAR
The lowercase of string is given=placement preparation

Program 11:

Write a program to split the string using the comma & space.

Suppose string is given={“suman”,”kumar”};//This is comma string

Suppose string is given=”amit kumar”;//This is space string

public class SplitStringByCommaAndSpace
{
public static void main(String[] arg)
{
String name={"suman","kumar"};
String[] array=name.split(",");
for(int i=0;i<array.lenght;i++)
{
System.out.println("Comma string is given= "+array[i]);
}
String name1="amit kumar";
String[] array1=name1.split("\\s+");
for(int i=0;i<array1.lenght;i++)
{
System.out.println("Space string is given= "+array1[i]);
}
}
}

Output:

Comma string is given=suman kumar
Space string is given=amit kumar

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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