Test String
Test String
Test String
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
//String x="IT WAS NOT TOUGH FOR HIM TO RESIDE ABOVE THE HILL";
System.out.println("Enter a Sentence");
String x=sc.nextLine();
x=x.toUpperCase();
int i,count=0;
for(i=0;i<x.length()-1;i++)
{
int ch1=x.charAt(i);
int ch2=x.charAt(i+1);
if(ch2==ch1+1)
count++;
}
System.out.println("No of consecutive pairs of charaters ="+count);
}
}
2)
class overload
{
public static void check(String s ,char ch)
{
int c=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==ch)
c++;
}
System.out.println("number of "+ ch+ " present is="+c);
}
public static void check(String s1)
{
s1=s1.toLowerCase();
for(int i=0;i<s1.length();i++)
{
char ch = s1.charAt(i);
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
System.out.print(ch+" ");
}
}
public static void main(String arg[])
{
check("success", 's');
check("computer");
}
}
3)Wap in Java to accept a string in lower case and change the first letter of every word to
upper case. Display the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Sentence");
String x=sc.nextLine();
x=x.toLowerCase();
String n="";
int i;
for(i=0;i<x.length();i++)
{
if(i==0 && x.charAt(i)!=' ')
n=n+Character.toUpperCase(x.charAt(i));
else if(x.charAt(i)==' ')
{
n=n+" "+Character.toUpperCase(x.charAt(i+1));
i++;
}
else
n=n+x.charAt(i);
}
System.out.print(n);
}
}
4)Wap to input a sentence and convert it into uppercase and count and display the total
number of words starting with a letter 'A'.
import java.util.*;
class Quest_7
{
void joystring(String s,char ch1,char ch2)
{
String str=s.replace(ch1,ch2);
System.out.println(str);
}
void joystring(String s)
{
int first=s.indexOf(' ');
System.out.println("First index :"+first);
int last=s.lastIndexOf(' ');
System.out.println("Last index :"+last);
}
void joystring(String s1,String s2)
{ String s3=" ";
String str=s1.concat(s3).concat(s2);
System.out.println(str);
}
public static void main(String args[])
{
Quest_7 obj=new Quest_7();
obj.joystring("TECHNALAGY",'A','O');
obj.joystring("Cloud computing means Internet based computing");
obj.joystring("COMMOM WEALTH","GAMES");
}
}
6)
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter file name and path: ");
String x=sc.nextLine();
//"C:\Users\admin\Pictures\flower.jpg";
int indexOfLastBackslash = x.lastIndexOf('\\');
int indexOfDot = x.lastIndexOf('.');
String outputPath = x.substring(0, indexOfLastBackslash + 1);
String fileName = x.substring(indexOfLastBackslash + 1, indexOfDot);
String extension = x.substring(indexOfDot + 1);
System.out.println("Path: " + outputPath);
System.out.println("File Name: " + fileName);
System.out.println("Extension: " + extension);
}
}
7) Define a class to accept two strings of same length and form a new word in such a way
that, the first character of the first word is followed by the first character of the second
word and so on.
Input string 1-BALL
OUTPUT: BWAOLRLD
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String a="BALL";
String b="WORD";
String c=" ";
int i;
for(i=0;i<a.length();i++)
{
c=c+a.charAt(i)+b.charAt(i);
}
System.out.println(c);
}
}
8)
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter word");
String x=sc.nextLine();
x=x.toUpperCase();
int i;
for(i=0;i<x.length();i++)
{
char ch=x.charAt(i);
if(ch=='A' || ch=='E'||ch=='I' || ch=='O' || ch=='U')
break;
}
System.out.println(x.substring(i)+x.substring(0,i)+"AY");
}
}
14) Write a program to accept a string. Convert the string to uppercase. Count
and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter word");
String x=sc.nextLine();
x=x.toUpperCase();
int i,count=0;
for(i=0;i<x.length()-1;i++)
{
if(x.charAt(i)==x.charAt(i+1))
count++;
}
System.out.println(count);
}
}
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter word");
String x=sc.nextLine();
x=x.toLowerCase();
String ans="";
int i;
for(i=0;i<x.length();i++)
{
char c = x.charAt(i);
if(c== 'a' || c== 'e' ||c == 'i' || c== 'o'||c== 'u')
ans=ans+(char)(c+1);
else
ans=ans+c;
}
System.out.println(ans);
}
}
15) Write a program in java to accept a string/word and display the new
string after removing all the vowels present in it.
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String x=sc.nextLine();
String n=" ";
int i;
for(i=0;i<x.length();i++)
{
char ch=x.charAt(i);
if(ch!='A' && ch!='E' && ch!='I' && ch!='O' && ch!='U' && ch!='a' && ch!='e'
&& ch!='i' && ch!='o' && ch!='u')
n=n+ch;
}
System.out.println(n);
}
}
16) Write a program to input a sentence and print the number of characters
found in the longest word of the given sentence.
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String x="india is country my";
x=x+"";
String n="",y="";
int i,max=0;
for(i=0;i<x.length();i++)
{
char ch=x.charAt(i);
if(ch!=' ')
n=n+ch;
else
{
if(n.length()>max)
{
max=n.length();
y=n;
}
n="";
}
}
System.out.println("longest word="+y);
System.out.println("Length of longest word="+max);
}
}
}
Write a program in java to accept a string/word and display the new string
after removing all
the vowels present in it.
i/p: COMPUTER APPLICATIONS
o/p: CMPTR PPLCTNS **/
import java.util.*;
class abc
{
public static void main(String args[])
{
String w="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String st=sc.nextLine();
for(int i=0;i<st.length();i++)
{
char x=st.charAt(i);
if(x!='A' && x!='E' && x!='I' && x!='O' && x!='U' && x!='a' && x!='e' &&
x!='i' &&x!='o' && x!='u')
w=w+x;
}
System.out.println(w);
}
}
** Write a program to accept a word and display the ASCII of each character.
Sample i/p: BLUEJ
Sample o/p: ASCII of B = 66
ASCII of L = 75
ASCII OF U = 84
ASCII OF E = 69
ASCII of L = 73 * */
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a word");
String st=sc.next();
for(int i=0;i<=st.length()-1;i++)
{
char x=st.charAt(i);
System.out.println("ASCII of "+x+" = "+(int)x);
}
}
}
/**Write a program to input a string in upper case and replace all the vowels
with Asterisk(*)
present in the string.
Sample i/p: TATA STEEL IS IN JAMSHEDPUR
Sample o/p: T*T* ST**L I* J*MSH*DP*R * */
import java.util.*;
class abc
{
public static void main(String args[])
{
int c=0;
String w="";
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.toUpperCase();
for(int i=0;i<=st.length()-1;i++)
{
char x=st.charAt(i);
if(x=='A'||x=='E'||x=='I'||x=='O'||x=='U')
{
x='*';
}
w=w+x;
}
System.out.println(w);
}
}
/** Write a program in java to enter a string and frame a word by joining all
the first characters
of each word.Display the new word.
Sample i/p: Vital Information Resource Under Seize
Sample o/p: VIRUS */
Write a program in java to enter a string and display all the palindrome words
present in the String.
Sample i/p: MOM AND DAD ARE NOT AT HOME
Sample o/p: MOM
DAD
import java.util.*;
class abc
{
public static void main(String args[])
{
char x;
String s1="",s2="",rev="";
System.out.println("\f");
Scanner sc=new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st= st.toUpperCase();
st=st.trim();
st=st+" ";
for(int i=0;i<st.length();i++)
{
x=st.charAt(i);
if(x!=' ')
{
s1=s1+x;
s2=x+s2;
}
else
{
if(s1.equals(s2))
System.out.println(s1);
s1="";
s2="";
}
}
}
}
/** Write a program to accept a string and display the string in a reverse
order.
Sample i/p: COMPUTER IS FUN
Sample o/p: FUN IS COMPUTER
import java.util.*;
class abc
{
public static void main(String args[])
{
String w="",s1="";
char x=' ';
Scanner sc= new Scanner(System.in);
System.out.println("enter a String:");
String st= sc.nextLine();
st=st.trim();
st=" "+st;
for(int i=st.length()-1;i>=0;i--)
{
x=st.charAt(i);
if(x!=' ')
w=x+w;
else
{
s1=s1+w+" ";
w=" ";
}
}
System.out.println(s1);
}
}
import java.util.*;
class abc
{
public static void main(String args[])
{
char x;
int c=0,f=0,l=0;
String w="",max="";
Scanner sc =new Scanner(System.in);
System.out.println("enter a string");
String st=sc.nextLine();
st=st.trim();
st=st+" ";
for(int i=0;i<st.length();i++)
{
x=st.charAt(i);
if(x!=' ')
{
w=w+x;
if(x=='a'||x=='A'||x=='e'||x=='E'||x=='i'||x=='I'||x=='o'||
x=='O'||x=='u'||x=='U')
c++;
}
else
{
if(f==0)
{
max=w;
l=c;
f=1;
}
else if(c>l)
{
max=w;
l=c;
}
w=" ";
c=0;
}
}
System.out.println("The word with maximum no of vowels :"+ max);
}
}
** Consider the string as “Blue bottle is in Blue bag lying on the Blue carpet”.
Write a program to accept a sample string and replace the word 'Blue' with
'red'. The new string is displayed as
“Red bottle is in Red bag lying on the Red carpet”
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string:");
String st=sc.nextLine();
st=st.replace("Blue","Red");
System.out.println(st);
}
}
}
**A string is said to be 'unique' if none of the alphabets present in the string
are repeated. Wap to accept a string and check whether the string is unique
or not. The program displays a message accordingly.
Sample i/p: COMPUTER
SAMPLE O/P: UNIQUE STRING */
import java.util.*;
class abc
{
public static void main(String args[])
{
int f=0;
char x;
Scanner sc= new Scanner(System.in);
System.out.println("enter a string:");
String st=sc.nextLine();
for(int i=0;i<st.length();i++)
{
x=st.charAt(i);
if(st.indexOf(x)!=st.lastIndexOf(x))
{
System.out.println("NOT A UNIQUE STRING");
f=1;
break;
}
}
if(f==0)
System.out.println("UNIQUE STRING");
}
}
BLUEJ
BLUE
BLU
BL
B
import java.util.*;
class abc
{
public static void main(String args[])
{
String str="BLUEJ";
int n=str.length();
for(int i=n;i>=0;i--)
{
System.out.println(str.substring(0,i));
}
}
}
J
E E
UUU
L L L L
B B B B B
import java.util.*;
class abc
{
public static void main(String args[])
{
String str="BLUEJ";
int n=str.length();
int k=0;
for(int i=4;i>=0;i--)
{
for(int j=0;j<=k;j++)
{
System.out.print(str.charAt(i));
}
System.out.println();
k++;
}
}
}
BLUEJ
LUEJ
UEJ
EJ
J
import java.util.*;
class abc
{
public static void main(String args[])
{
String str="BLUEJ";
int n,i,k=0;
n=str.length();
for(i=0;i<n;i++)
{
System.out.println(str.substring(k,n));
k++;
}
}
}
Define a class to accept a String and print the number of digits, alphabets and
special characters in the string.
Example: = “KAPILDEV@83”
Output: Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1