0% found this document useful (0 votes)
5 views3 pages

Java String Manipulation Programs

Dchytd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Java String Manipulation Programs

Dchytd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
class stringprograms
{
public void palindrome(){
Scanner scanner = new Scanner([Link]);
[Link]("Enter the string: ");
String s= [Link]();
String rev = "";
for (int i = [Link]()-1; i >=0 ; i--)
rev=rev+[Link](i);
if([Link](rev))
[Link]("String is palindrome");
else
[Link]("String is not palindrome");
[Link]();
}

public void stringfrequency()


{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the string: ");
String s= [Link]();

[Link]("Enter the word to search: ");


String word= [Link]();
String temp[] = [Link](" ");
int count = 0;
for (int i = 0; i < [Link]; i++)
{
if ([Link](temp[i]))
count++;
}
[Link]("The string is: " + s);
[Link]("The word " + word + " occurs " + count + "
times in the above string");
[Link]();
}

public void replace()


{

Scanner scanner = new Scanner([Link]);


[Link]("Enter the original string: ");
String s= [Link]();

[Link]("Enter the substring to be replaced: ");


String oldsubstring = [Link]();

[Link]("Enter the new substring: ");


String newsubstring = [Link]();

String modifiedstring = [Link](oldsubstring, newsubstring);


[Link]("Modified string: " + modifiedstring);

[Link]();
}

public void sortstring( )


{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the string: ");
String s1= [Link]();

// Converting input string to character array


char s2[] = [Link]();

// Sort
[Link](s2);

[Link]("Input String : " + s1);


[Link]("Output String : ");
for(char ch:s2)
[Link](ch);
[Link]();
[Link]();
}

public void compare_concat()


{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the string1: ");
String s1= [Link]();
[Link]("Enter the string2:");
String s2= [Link]();
[Link]();
boolean res1 = [Link](s1);

if(res1)
[Link]("str2 is equal to str1");
else
[Link]("str2 is equal not to str1");

//concatenation
String s3 = [Link](s2);

[Link]("concatenated string is:"+s3);

}
}

class stringdemo{
public static void main(String[] args){
Scanner scanner = new Scanner([Link]);
stringprograms str=new stringprograms();

[Link]("Choices:");
[Link]("1-paindrome");
[Link]("2-stringfrequency");
[Link]("3-Replace the substring");
[Link]("4-sort");
[Link]("5-Compare and Concatenate");
[Link]("Enter your choice:");
int choice=[Link]();
switch(choice)
{
case 1: [Link]();
break;
case 2: [Link]();
break;
case 3: [Link]();
break;
case 4: [Link]( );
break;
case 5:str.compare_concat();
break;
default:[Link]("Invaid choice");
break;
}

}
}

You might also like