PROGRAM BASED ON STRING MANIPULATIONS
Question 1: Write a program in Java to accept a word and display the same in
Pig Latin form. A word is framed in Pig Lati form by using the following steps:
i. Enter a Word
ii. Shift all
letters which are available before the first vowel, towards the end of the
word.
iii. Finally, add
'AY' at the end of the word sequence. [ICSE 2013]
Sample
Input: TROUBLE
Sample Output:
OUBLETRAY
Solution:-
/// display a Pig
Latin Word
public class ConvertPigLatin
{
public static void main(String args[])
{
System.out.println("The Pig Latin form of the given string");
for(y=0;y<x;y++)
{
b=(str.charAt(y));
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'||b=='A'||b=='E'||b=='I'||b=='O'||b=='U')
break;
}
str1=str.substring(y,x);
str2=str.substring(0,y);
System.out.println(str1+str2+"ay");
}
Question 2: Write a program in Java to enter a String. Prnt the String in alphabetical order of its letters.
Sample Input: COMPUTER
Sample Output: CEMOPRTU
Solution:-
/// To arrange the letters of String in alphabetical order
public class Letters
{
public static void main(String args[])
{
int i,j,p;
char ch;
p=str.length();
System.out.println("The Letters arranged in alphabetical order....");
for(i=65;i<=90;i++)
{
for(j=0;j<p;j++)
{
ch=str.charAt(j);
if(ch==(char)i||(char)(i+32)
System.out.println(ch);
}
}
}
Sample Input: we are in cyber world
Sample Output: We Are In Cyber World [ICSE 2018]
Solution:-
public class ConvertSentenceCase
{
public static void main(String args[])
{
String str1="";
int i,p;
char chr,chr1;
str=' '+str;
p=str.length();
{
chr=str.charAt(i);
if(chr==' ')
{
char1=str.charAt(i+1);
str=str1+' '+Character.tiUpperCase(chr1);
i=j+1;
else
str1=str1+chr;
}
System.out.println(""The new String after changing the first letter of the word:");
System.out.println(str1);
}
}
Question 4: Write a program to input a sentence and it into uppercase and count and display the total number of words starting with a letter 'A'.
[ICSE 2019]
Example:
Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.
Sample Output: Total number of words starting with letter 'A' = 4.
Ans.
import java.util.";
import java.util.Scanner;
public class WordsInSentence
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
char ch int i;
System.out.println("Type a sentence: ");
String sentence = scan.nextLine();
System.out.println("You typed: "+ sentence.toUpperCase());
String capsen=sentence.toUpperCase():
int count=0;
char ch[]= new char[capsen.length()];
for(String st: capsen.split(" "))
{
if(st.startsWith("A"))
{
count++;
System.out.println(st);
}
}
System.out.println("Total Number of words starting with '\A'\ is : "+coount);
}
}
Question 5: Write a program to input a string and print out the text with te uppercase andlower case letters reversed, but all other characters should be remain the same as before.
Example:
Input: The girl IS DancInG
Output:tHE GIRL is dANCiNg
Solution:-
import java.util.";
public class ULToggle
{
public static void main(String args[])
{
//user input
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence : ");
String sentence=sc.nextLine();
//toggle the case
String result="";
char ch;
int i;
for(i=0;i<sentence.length();i++)
{
ch=sentence.charAt(i);
if(Character.isUpperCase(ch))
ch=Character.toLowerCase(ch);
else
ch=Character.toUpperCase(ch));
result=result+ch;
System.out.println("Result :"+result);
}
}
import java.util.";
public class FindLongestWord
{
public static void main(String args[])
{
//user input
char ch;
str=str+" ";
int indx=0;
int x=str.length();
System.out.println("Enter a sentence : "+str);
System.out.println("\n");
String f=" ";
for(int i=0;i<x;i++)
{
ch=str.charAt(i);
if(ch==' ')
{
import java.io.*;
import java.util.";
public class CharacterDigit
{
void replaceString() throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.println("String is : ");
String sentence=sc.nextLine();
char x=n.charAt(4);
String chr=n.replace(x, 5);
System.out.println("The output string is :"+chr);
}
public static void main(String args[]) throws IOException
{
CharacterDigit c=new CharacterDigit();
c.replaceString();
}
}
No comments:
Post a Comment