0% found this document useful (0 votes)
6 views31 pages

Java String Manipulation Examples

The document contains multiple Java programs focused on string handling, including functionalities like counting words and letters, removing vowels, generating initials, reversing strings, and identifying palindromes. Each program is structured with a main method that utilizes the Scanner class for user input and processes the string accordingly. The programs demonstrate various string manipulation techniques and algorithms in Java.

Uploaded by

Vasanth
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)
6 views31 pages

Java String Manipulation Examples

The document contains multiple Java programs focused on string handling, including functionalities like counting words and letters, removing vowels, generating initials, reversing strings, and identifying palindromes. Each program is structured with a main method that utilizes the Scanner class for user input and processes the string accordingly. The programs demonstrate various string manipulation techniques and algorithms in Java.

Uploaded by

Vasanth
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

STRING HANDLING

import [Link].*;

public class Pg289_1

public static void main()

Scanner in=new Scanner([Link]);

String x;

int i,cnt1=0,cnt2=0;

[Link]("Enter a sentence : ");

x=[Link]();

for(i=0;i<[Link]();i++)

if([Link]([Link](i)))

cnt1++;

else

cnt2++;

[Link]("No. of words : "+ ++cnt1);

[Link]("No. of Letters : "+ cnt2);

import [Link].*;

public class Pg289_2

public static void main()

1
Scanner in=new Scanner([Link]);

String x,y="";

char z;

int i;

[Link]("Enter a word : ");

x=[Link]().toLowerCase();

for(i=0;i<[Link]();i++)

z=[Link](i);

if(z!='a'&&z!='e'&&z!='i'&&z!='o'&&z!='u')

y=y+z;

[Link]("Word without vowel : "+y);

import [Link].*;

public class Pg290_3

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

char z;

int i;

[Link]("Enter your full name : ");

x=[Link]().toUpperCase();

2
z=[Link](0);

y=y+z;

for(i=1;i<[Link]();i++)

z=[Link](i);

if([Link](z))

y=y+" "+[Link](i+1);

[Link]("Initial : "+y);

import [Link].*;

public class Pg290_4

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

char z;

int i,pos;

[Link]("Enter your full name : ");

x=[Link]().toUpperCase();

pos=[Link](" ");

y=[Link](pos+1);

y=y+" "+[Link](0,pos);

3
[Link]("Name : "+y);

import [Link].*;

public class Pg290_5

public static void main()

Scanner in=new Scanner([Link]);

String x,y="",z="";

char c;

int i,pos,len=0,max=0;

[Link]("Enter a sentence : ");

x=[Link]().toUpperCase();

x=x+" ";

for(i=0;i<[Link]();i++)

c=[Link](i);

if (c!= ' ')

y=y+c;

else

len=[Link]();

[Link]("Length :"+len+"\tWord : "+y);

if (len>max)

max=len;

4
z=y;

y="";

[Link]("\nLongest Word : "+z+"\tLength :"+max);

import [Link].*;

public class Pg290_6

public static void main()

Scanner in=new Scanner([Link]);

String x;

char z;

int i;

[Link]("Enter a word : ");

x=[Link]();

for(i=0;i<[Link]();i++)

z=[Link](i);

[Link]("ASCII of "+z+" = "+(int)z);

5
import [Link].*;

public class Pg290_7

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

char z;

int i;

[Link]("Enter a String : ");

x=[Link]().toUpperCase();

for(i=0;i<[Link]();i++)

z=[Link](i);

if(z=='A'||z=='E'||z=='I'||z=='O'||z=='U')

y=y+"*";

else

y=y+z;

[Link]("Output : "+y);

import [Link].*;

public class Pg290_8

public static void main()

6
Scanner in=new Scanner([Link]);

String x,y="";

char z;

int i;

[Link]("Enter a sentence : ");

x=[Link]();

z=[Link](0);

y=y+z;

for(i=1;i<[Link]();i++)

z=[Link](i);

if([Link](z))

y=y+[Link](i+1);

[Link]("SHORT FORM : "+y);

import [Link].*;

public class Pg290_9

public static void main()

Scanner in=new Scanner([Link]);

String x,x1="",y="";

char z,c;

int i,j,pos;

[Link]("Enter a sentence : ");

7
x=[Link]()+" ";

for(i=0;i<[Link]();i++)

c=[Link](i);

if(c!=' ')

x1=x1+c;

else

y="";

for(j=[Link]()-1;j>=0;j--)

z=[Link](j);

y=y+z;

if([Link](y))

[Link](x1+" is a Palindrome");

x1="";

import [Link].*;

public class Pg290_10

public static void main()

Scanner in=new Scanner([Link]);

String x,y;

int i,pos;

8
[Link]("Enter a sentence : ");

x=" "+[Link]();

[Link]("Output : ");

for(i=[Link]()-1;i>=0;)

pos=[Link](" ",i);

y=[Link](pos,i+1);

i=pos-1;

[Link](y);

import [Link].*;

public class Pg290_11

public static void main()

Scanner in=new Scanner([Link]);

String x,y="",max_word="";

char z,c;

int i,j,pos,max=0,cnt;

[Link]("Enter a word : ");

x=[Link]().toUpperCase()+" ";

for(i=0;i<[Link]();i++)

c=[Link](i);

9
if(c!=' ')

y=y+c;

else

cnt=0;

for(j=0;j<[Link]();j++)

z=[Link](j);

if(z=='A'||z=='E'||z=='I'||z=='O'||z=='U')

cnt++;

if(cnt>max)

max=cnt;

max_word=y;

y="";

[Link]("The word with maximum no. of vowels : "+max_word);

import [Link].*;

public class Pg290_12

public static void main()

String x="Blue bottle is in Blue bag lying on Blue carpet ",y="",z="";

10
char c;

int i,j;

for(i=0;i<[Link]();i++)

c=[Link](i);

if(c!=' ')

z=z+c;

else

if([Link]("BLUE"))

y=y+"Red ";

else

y=y+z+" ";

z="";

[Link]("Given Sentence : "+x);

[Link]("After Replace : "+y);

import [Link].*;

public class Pg290_13

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

char z;

11
int i;

[Link]("Enter a word : ");

x=[Link]().toLowerCase();

for(i=0;i<[Link]();i++)

z=[Link](i);

if(z=='a'||z=='e'||z=='i'||z=='o'||z=='u')

x=[Link](z,(char)(1+z));

[Link]("Output : "+x);

import [Link].*;

public class Pg291_14

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

char z,c;

int i;

[Link]("Enter a word : ");

x=[Link]().toUpperCase();

for(i=0;i<[Link]();i++)

12
z=[Link](i);

if(z!='A'&&z!='E'&&z!='I'&&z!='O'&&z!='U')

c=(char)(z-1);

if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')

y=y+(char)(z+1);

else

y=y+c;

else y=y+z;

[Link]("Output : "+y);

import [Link].*;

public class Pg291_15

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

int i,n1,n2,r,s;

[Link]("Enter a word : ");

x=[Link]().toUpperCase();

[Link]("X = "+x);

for(i=0;i<[Link]();i++)

13
y=y+(int)([Link](i)-64);

[Link]("Converted to No. : "+y);

n1=[Link](y);

n2=n1;

while(n1>9)

s=0;

while(n1>0)

r=n1%10;

s=s+r*r;

n1=n1/10;

n1=s;

[Link]("N1 = "+n1);

if(n1==1)

[Link]("Given word is a Happy Word");

else

[Link]("Given word is not a Happy Word");

import [Link].*;

public class Pg291_16

public static void main()

14
{

Scanner in=new Scanner([Link]);

String x;

char i;

int j,cnt;

[Link]("Enter a sentence : ");

x=[Link]().toUpperCase();

[Link]("===================");

[Link]("Character Frequency");

[Link]("===================");

for(i='A';i<='Z';i++)

cnt=0;

for(j=0;j<[Link]();j++)

if(i==[Link](j))

cnt++;

if(cnt>0)

[Link](" "+i+" "+cnt);

import [Link].*;

public class Pg291_17

public static void main()

Scanner in=new Scanner([Link]);

15
String x;

int i,cnt=0;

[Link]("Enter a sentence : ");

x=[Link]().toUpperCase()+" ";

for(i=0;i<[Link]()-1;i++)

if([Link](i)==[Link](i+1))

cnt++;

i++;

[Link]("No. of double letter sequences : "+cnt);

import [Link].*;

public class Pg291_18

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

int i,j;

[Link]("Enter a word : ");

x=[Link]();

16
for(i=[Link]()-1;i>=0;i--)

y=y+[Link](i);

[Link]("Reversed String : "+y);

if([Link](0)==[Link]([Link]()-1))

if([Link](y))

[Link]("Given word is a Palindrome & Special word");

else

[Link]("Given word is not a Palindrome but a Special word");

else [Link]("Given word is neither a Palindrome nor a Special word");

import [Link].*;

public class Pg291_19

public static void main()

Scanner in=new Scanner([Link]);

String x,y="";

char c;

int i,j,cnt=0;

[Link]("Enter a string : ");

x=[Link]().toUpperCase()+" ";

for(i=0;i<[Link]();i++)

17
c=[Link](i);

if(c!=' ')

y=y+c;

else

for(j=0;j<[Link]()-1;j++)

if((1+[Link](j))==[Link](j+1))

[Link](y);

cnt++;

y="";

[Link]("No. of words containing consecutive letters : "+cnt);

import [Link].*;

public class Pg292_20

public static void main()

Scanner in=new Scanner([Link]);

String x;

int i,j;

[Link]("Enter a word : ");

x=[Link]().toUpperCase();

18
[Link]("\n\nPattern 1");

for(i=[Link]()-1;i>=0;i--)

for(j=0;j<=i;j++)

[Link]([Link](j)+" ");

[Link]();

[Link]("\n\nPattern 2");

for(i=[Link]()-1;i>=0;i--)

for(j=[Link]()-1;j>=i;j--)

[Link]([Link](i)+" ");

[Link]();

[Link]("\n\nPattern 3");

for(i=[Link]()-1;i>=0;i--)

for(j=[Link]()-1-i;j<[Link]();j++)

[Link]([Link](j)+" ");

[Link]();

public class Pg292_21

public static void main()

19
char x,y;

int i,j;

[Link]("\n\nPattern 1");

y='A';

for(i=1;i<=5;i++)

x=y;

for(j=i;j<=5;j++)

[Link](x++ +" ");

y++;

[Link]();

[Link]("\n\nPattern 2");

x='A';

for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

[Link](x++ +" ");

[Link]();

[Link]("\n\nPattern 3");

for(i=1;i<=5;i++)

x='A';

for(j=i;j<=5;j++)

[Link](x++ +" ");

x='A';

for(j=2;j<=i;j++)

20
[Link](x++ +" ");

[Link]();

import [Link].*;

public class Pg292_22

public static void main()

Scanner in=new Scanner([Link]);

char x='*';

int i,j,ch,n;

[Link]("1. Triangle");

[Link]("2. Inverted Triangle");

[Link]("Enter your choice : ");

ch=[Link]();

[Link]("Enter n : ");

n=[Link]();

switch(ch)

case 1:

[Link]("\n\nPattern 1");

for(i=1;i<=n;i++)

for(j=2;j<=i;j++)

[Link](" ");

21
for(j=i;j<=n;j++)

[Link](x);

[Link]();

break;

case 2:

[Link]("\n\nPattern 2");

for(i=1;i<=n;i++)

x='A';

for(j=i;j<=n;j++)

[Link](x++);

[Link]();

break;

default: [Link]("Invalid Choice");

import [Link].*;

public class Pg292_23

public static void main()

Scanner in=new Scanner([Link]);

String x="BLUEJ";

int i,j,ch;

[Link]("1. Triangle");

[Link]("2. Inverted Triangle");

22
[Link]("Enter your choice : ");

ch=[Link]();

switch(ch)

case 1:

[Link]("\n\nPattern 1");

for(i=0;i<[Link]();i++)

for(j=0;j<=i;j++)

[Link]([Link](i)+" ");

[Link]();

break;

case 2:

[Link]("\n\nPattern 2");

for(i=0;i<[Link]();i++)

for(j=0;j<[Link]()-i;j++)

[Link]([Link](j)+" ");

[Link]();

break;

default: [Link]("Invalid Choice");

23
import [Link].*;

public class Pg292_24

public static void main()

Scanner in=new Scanner([Link]);

String x="ICSE";

int i,j,ch,n=1;

[Link]("1. Floyd's Triangle");

[Link]("2. Triangle");

[Link]("Enter your choice : ");

ch=[Link]();

switch(ch)

case 1:

[Link]("\n\nPattern 1");

for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

[Link](n++);

[Link]();

break;

case 2:

[Link]("\n\nPattern 2");

for(i=0;i<[Link]();i++)

for(j=0;j<=i;j++)

[Link]([Link](j)+" ");

24
[Link]();

break;

default: [Link]("Invalid Choice");

public class Pg293_25

public static void main()

String
country[]={"Brazil","China","England","France","Germany","India","Iraq","Jamaica","UK","USA"};

String capital[]={"Brasilia","Beijing","London","Paris","Berlin","New
Delhi","Badhdad","Kingston","London","Washington D.C."};

char c;

int i;

[Link]("\n\n Country Names that starts with vowel");

[Link](" ------------------------------------");

[Link]("\n Country Names Capital");

for(i=0;i<[Link];i++)

c=country[i].toUpperCase().charAt(0);

if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')

[Link]("\t"+country[i]+"\t\t\t"+capital[i]);

25
import [Link].*;

public class Pg293_26

public static void main()

Scanner in=new Scanner([Link]);

int i,j,n=10,min_p;

char x;

String min_v,t;

String a[]={"A2","A3","A1","D4","A5","F7","A6","B10","A9","A8"};

String
b[]={"9898000002","9898000003","9898000001","9898000004","9898000005","9898000007","989
8000006","9898000010","9898000009","9898000008"};

[Link]("Enter a letter : ");

x=[Link]().charAt(0);

for(i=0;i<n;i++)

min_v=a[i];

min_p=i;

for(j=i+1;j<n;j++)

if(a[j].compareTo(min_v)<0)

min_v=a[j];

min_p=j;

t=a[i];

a[i]=min_v;

a[min_p]=t;

26
t=b[i];

b[i]=b[min_p];

b[min_p]=t;

[Link]("Output");

for(i=0;i<n;i++)

if(a[i].charAt(0)==x)

[Link](a[i]+"\t"+b[i]);

import [Link].*;

public class Pg293_27

public static void main()

Scanner in=new Scanner([Link]);

int i,j,n;

String t;

String a[]=new String[20];

String b[]=new String[20];

[Link]("Enter n : ");

n=[Link]();

[Link]("Enter the Array values : ");

for(i=0;i<n;i++)

a[i]=[Link]().toUpperCase();

b[i]=a[i];

27
}

for(i=0;i<n-1;i++)

for(j=0;j<n-1;j++)

if(a[j+1].compareTo(a[j])<0)

t=a[j];

a[j]=a[j+1];

a[j+1]=t;

[Link]("Input Sorted Array");

for(i=0;i<n;i++)

[Link](b[i]+"\t "+a[i]);

import [Link].*;

public class Pg293_28

public static void main()

Scanner in=new Scanner([Link]);

int i,n=10;

String a[]=new String[n];

char x,y;

28
[Link]("Enter city names");

for(i=0;i<n;i++)

a[i]=[Link]().toUpperCase();

[Link]("Output");

for(i=0;i<n;i++)

x=a[i].charAt(0);

y=a[i].charAt(a[i].length()-1);

if(x!='A'&&x!='E'&&x!='I'&&x!='O'&&x!='U')

if(y=='A'||y=='E'||y=='I'||y=='O'||y=='U')

[Link](a[i]);

import [Link].*;

public class Pg293_29

public static void main()

Scanner in=new Scanner([Link]);

int i,j,n=5;

String x[]=new String[n];

String y;

[Link]("Enter words");

for(i=0;i<n;i++)

29
x[i]=[Link]().toUpperCase();

[Link]("\n\nPalindrome Words\n");

for(i=0;i<n;i++)

y="";

for(j=x[i].length()-1;j>=0;j--)

y=y+x[i].charAt(j);

if(x[i].equalsIgnoreCase(y))

[Link](x[i]);

import [Link].*;

public class Pg292_30

public static void main()

Scanner in=new Scanner([Link]);

int i,j,n=3;

String a[]=new String[n];

String b[]=new String[n];

String x,y="",z="";

boolean flag=false;

[Link]("Enter cities and STD");

for(i=0;i<n;i++)

30
a[i]=[Link]();

b[i]=[Link]();

[Link]("Enter city name to search : ");

x=[Link]();

for(i=0;i<n;i++)

if(a[i].equalsIgnoreCase(x))

flag=true;

y=a[i];

z=b[i];

break;

if(flag)

[Link]("Search Successful");

[Link](y+" "+z);

else [Link]("Search unsuccessful, no such city in the list");

31

Common questions

Powered by AI

The program described in Source 2 converts each vowel in a given word to the subsequent character in the ASCII sequence. It does so by checking if the character is a lowercase vowel ('a', 'e', 'i', 'o', 'u'), and if so, it replaces the vowel with the character obtained by adding one to its ASCII value using the `replace` method .

The program reads multiple words into an array, converts each word to its reverse by iterating backwards, and compares it with the original word using `equalsIgnoreCase`. If the reversed word matches the original, the word is identified as a palindrome and is printed. This process is significant because it demonstrates the ability to validate symmetrically structured data and the robustness of string manipulation in Java .

The program initializes a counter for each alphabet letter (A to Z) and iterates through the input string to compare each character with all letters in the alphabet. If a match is found, the respective counter is incremented. Once all characters are processed, the program checks if any counter exceeds zero, indicating the presence of that letter, and prints it with its frequency count. This process enables understanding of text analysis and frequency distribution using simple iteration and conditional statements .

The program takes a sentence as input, appends a space to ensure the last word is also processed, and iterates through each character. Each word is constructed by accumulating characters until a space is encountered, indicating the end of the word. The current word is then reversed by iterating backwards through the characters and appending them to a new string. Once reversed, it is checked for palindrome properties. The logic encapsulates reversing individual words to enhance understanding of string manipulations and palindrome detection .

Sorting city names based on their start and end characters facilitates the easy retrieval and distinction of cities that begin with consonants and end with vowels. The sorting is achieved by iteratively comparing and swapping adjacent elements if they are not in the desired order. This sorted output can enhance data processing efficiency and accuracy. Utilizing precise control structures emphasizes understanding of sorting algorithms and their application in organizing data sets based on specific criteria, improving dataset manipulation efficiency .

Reversing the word string serves a dual purpose: it aids in verifying palindrome properties (where a word reads the same forward and backward) and assists in determining 'Special Words' by checking if the first and last characters are identical. This dual-check is significant as it combines multiple logical conditions to classify word types based on structure, thus exemplifying advanced string handling techniques and logical operations in problem-solving .

The program reads a sentence and splits it into words. For each word, it checks every pair of consecutive characters to see if the ASCII value of the second character is exactly one more than the first, indicating they are consecutive alphabets. If such a pair is found, the word is printed, and a counter is incremented to track the number of words containing consecutive letters. This approach highlights complex string traversal and pattern recognition essential for linguistic analysis and data validation .

The program first initializes two arrays: one for country names and another for their corresponding capitals. It then iterates through the country names, converts the first letter to uppercase, and checks if it is a vowel ('A', 'E', 'I', 'O', 'U'). If the condition is satisfied, it prints the country name along with its capital from the parallel array .

A word is deemed 'Happy' if it results in a single-digit number 1, following specific processing. Each character in the word is converted into a number by taking its position in the alphabet (A=1, B=2, etc.). The resulting number is squared and summed repeatedly until a single-digit is obtained. If this number equals 1, the word is labeled a 'Happy Word'. This implementation incorporates character manipulation and iterative mathematical calculations to assess word properties, illustrating algorithmic thinking and numeric analysis .

The program's primary purpose is to count and display the number of words and the number of letters in a given sentence input by the user. It uses a Scanner object to read the input string, iterates through each character of the string to determine whether it is a whitespace or a letter, and accordingly increments the respective counters. The word count is determined by counting the number of whitespace characters and adding one to it, assuming that the sentence is space-separated .

You might also like