0% found this document useful (0 votes)
148 views9 pages

String Based Program

The document contains 5 problems on string manipulation in Java with solutions. The problems include: 1) Removing consecutive blank spaces in a string, 2) Forming a word from the first letter of each word in a sentence and manipulating it, 3) Checking for palindrome words in a string, 4) Counting characters needed to make a word a palindrome, and 5) Encrypting a word by incrementing/decrementing character codes. For each problem, the input, output, and Java code to solve it is given.

Uploaded by

Priyesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
148 views9 pages

String Based Program

The document contains 5 problems on string manipulation in Java with solutions. The problems include: 1) Removing consecutive blank spaces in a string, 2) Forming a word from the first letter of each word in a sentence and manipulating it, 3) Checking for palindrome words in a string, 4) Counting characters needed to make a word a palindrome, and 5) Encrypting a word by incrementing/decrementing character codes. For each problem, the input, output, and Java code to solve it is given.

Uploaded by

Priyesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

String based program

1. While typing a typewriter has made two or more consecutive blank spaces ina a sentence.
Write a program in java to replace two or more consecutive blanks by a single blank

input:

Understanding computer applications for ICSE is written by vijay


kumar pandey and Dilip Kumar Dey

output :

Understanding computer applications for ICSE is written by vijay kumar pandey and Dilip
Kumar Dey.

import java.util.*;

class space

public static void main(String arg [])

Scanner sc=new Scanner(System.in);

System.out.println("Enter any String");

String s=sc.nextLine();

s=s.replaceAll("( )+", " ");

System.out.println(s);

}
2. Write a program to input a sentence in uppercase and frame a word by adding the first
alphabet of each word and also arrange the alphabets of the new word in an alphabetical order
also remove the duplicating alphabets.

input : NARENDRA MODI IS THE PRIME MINISTER OF INDIA

output : NMITPMOI

The word in alphabetical order is : IIMMNOPT

After eliminating duplicate alphabets : IMNOPT

import java.util.*;

class s2

public static void main(String arg [])

Scanner sc=new Scanner(System.in);

System.out.println("Enter any String");

String s=sc.nextLine();

s=" " +s;

s=s.toUpperCase();

String w="", w1="",q="";

int len=s.length();

for(int i=0;i<len;i++)

char ch=s.charAt(i);

if(ch==' ')

w=w+s.charAt(i+1);
}

System.out.println("First letter of each word is "+w);

for (int i=65;i<=90;i++)

char ch=(char)i;

for(int j=0;j<w.length();j++)

char ch1=w.charAt(j);

if(ch1==ch)

w1=w1+ch1;

System.out.println("The word in aplhabetical order :"+w1);

w1=w1+" ";

int len=w1.length();

for (int i=0;i<len-1;i++)

char ch=w1.charAt(i);

if(ch==w1.charAt(i+1))

w2=w2+w1.charAt(i+1);

i++;
}

else

w2=w2+ch;

System.out.println("After eliminating dupliacte alphabets :"+w2);

3. Write a program in java to input a string and check the frequency of palindrome words(containing 3
or more characters) in the given string.

input: MADAM ARORA IS AN ENGLISH TEACHER

output : The three characters or more palindrome words are: MADAM ARORA

The frequency of palindrome words are : 2

import java.util.*;

class revcheck

public static void main(String arg [])

Scanner sc=new Scanner (System.in);

System.out.println("Enter the string");

String s=sc.nextLine();

s=s.toUpperCase() + " ";

int len=s.length();
String p="";

int c=0;

for(int i=0;i<len-1;)

char ch=s.charAt(i);

String w="";

while(ch!=' ')

ch=s.charAt(i);

w=w+ch;

i++;

if(w.length()>=3)

String r="";

for(int j=w.length()-1;j>=0;j--)

char ch1=w.charAt(j);

r=r+ch1;

w=w.trim();

r=r.trim();

if(w.equals(r))

c++;
p=p+w+" ";

w="";

System.out.println("Frequency of palindrome word : "+c);

System.out.println("The palindrome words are : "+p);

4. Write a program to accept a word and count the number of characters to make the word palindrome.

input : DILIP

output : Reverse word = PILID

Number of characters which differ to make palindrome word = 2

import java.util.*;

class Rev

public static void main(String arg [])

Scanner sc=new Scanner(System.in);

System.out.println("Enter any string");

String s=sc.nextLine();

int len=s.length();

String w="";
int c=0;

s=s.toUpperCase();

for(int i=len-1;i>=0;i--)

w=w+s.charAt(i);

for (int i=0;i<w.length();i++)

if(w.charAt(i)!=s.charAt(i))

c++;

System.out.println(" palindrome word :"+w);

System.out.println("Numbers of characters which differ to make palindrome word :"+c);

5. Write a program in java to accept a word and encrypt as follows:

eg. word encrypted form

Zeal xfbj

program nppepbk

anode blpbf

import java.util.*;
class encrypted

public static void main(String arg [])

Scanner sc=new Scanner(System.in);

System.out.println("Enter any String");

String s=sc.nextLine();

String w="";

int len=s.length();

for(int i=0;i<len;i++)

char ch=s.charAt(i);

if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

int m=((int)ch)+1;

w=w+(char)m;

else if(ch!='A'||ch!='E'||ch!='I'||ch!='O'||ch!='U'||ch!='a'||ch!='e'||ch!='i'||ch!='o'||ch!='u')

int m=((int)ch)-2;

w=w+(char)m;

System.out.println("Word"+"\t"+"Encrypted form");
System.out.println("****"+"\t"+"********* ****");

System.out.println(s+ "\t"+ w);

You might also like