Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Test String

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

1)WAP to accept a string. Convert the string to uppercase.

Count and output the


number of Consecutive letter pairs that exist in the string.
Sample Input: “IT WAS NOT TOUGH FOR HIM TO RESIDE ABOVE THE HILL”
Sample Output: Number of consecutive pair of characters = 6

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'.

Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY


ARE EVER CHANGING.
Output: Total number of words starting with letter 'A' = 4.
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String x="ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY
ARE EVER CHANGING";
x=x.toUpperCase();
int count=0,i;
for(i=0;i<x.length();i++)
{
if(i==0 && x.charAt(0)=='A')
count++;
if(x.charAt(i)==' '&& x.charAt(i+1)=='A')
count++;
}
System.out.println("Total number of words starting with letter A"+count);
}
}
5)

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

Input String 2- WORD

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 ques6
{
public static void main (String s[])//throws
IOException
{
Scanner obj = new Scanner(System.in);
System.out.println(" enter a word ");
String word = obj.nextLine();
String rword="";
int l,x;
l= word.length();
for(x=l-1;x>=0;x--)
rword = rword+ word.charAt(x);
if(word.equals(rword))
System.out.println( word +" is palindrome");
else
if(word.charAt(0) == word.charAt(l-1))
System.out.println( word +"is a special word");
}
}
9)
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String x=sc.nextLine();
x=x.toUpperCase();
int i,j,count;
System.out.println("CHARACTERS \t FREQUENCY");
for(i=65;i<=90;i++)
{
count=0;
for(j=0;j<x.length();j++)
{
if(i==x.charAt(j))
count++;
}
if(count>0)
System.out.println((char)i+"\t\t"+count);
}
}
}
10)Define a class to accept two strings, convert them into uppercase, check and display
whether two strings are equal or not, if the two strings are not equal, print the string with
the highest length or print the message both the strings are of equal length.
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 Strings");
String a=sc.next();
String b=sc.next();
a=a.toUpperCase();
b=b.toUpperCase();
if(a.length()!=b.length())
{
if(a.length()>b.length())
System.out.println(a);
else
System.out.println(b);
}
else
System.out.println("Both the strings are equal length");
}
}
11) Write a program to input a string and convert it into uppercase and print the pair of
vowels and number of pair of vowels occurring in the string.
Example:
Input: "BEAUTIFUL BEAUTIES "
Output :
Pair of vowels: EA, AU, EA, AU, IE
No. of pair of vowels: 5
import java.util.*;
class abc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String x=sc.nextLine();
x=x.toUpperCase();
int count=0,i;
for(i=0;i<x.length()-1;i++)
{
char ch1=x.charAt(i);
char ch2=x.charAt(i+1);
if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U')&& (ch2=='A' || ch2=='E' ||
ch2=='I' || ch2=='O' || ch2=='U'))
{
count++;
System.out.print(" "+ch1+ch2);
}
}
System.out.println("No. of pair of vowels:"+count);
}
}
13)Write a program that encodes a word into Piglatin. To translate word into
Piglatin word, convert the word into uppercase and then place the first vowel
of the original word as the start of the new word along with the remaining
alphabets. The alphabets present before the vowel being shifted towards the
end followed by “AY”.
Sample Input(1): London Sample Output(1): ONDONLAY
Sample Input(2): Olympics Sample Output(2): OLYMPICSAY

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);
}
}

15) Write a program to accept a word and convert it into lowercase if it is in


uppercase, and display the new word by replacing only the vowels with the
character following it.
Example
Sample Input : computer
Sample Output : cpmpvtfr

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.

Input : COMPUTER APPLICATIONS


Output : CMPTR PPLCTNS

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.

For example is S = “India is my country” then the output should be 7

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);
}
}

/**Write a program to input a string and print the word containing


maximum no of vowels.
SAMPLE i/p: HAPPY NEW YEAR
SAMPLE O/P: THE WORD WITH MAXIMUM NO OF VOWELS :
YEAR */

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

You might also like