0% found this document useful (0 votes)
18 views

class 11 COMPUTER PROJECT solution

The document outlines a series of Java programming tasks that involve string manipulation, including creating acronyms, converting sentences to Pig Latin, reversing words, sorting words by length, and identifying character frequencies. Each task includes sample input and output, along with the corresponding Java code to achieve the desired results. The document serves as a comprehensive guide for students to practice their Java programming skills related to string handling.

Uploaded by

jeeaspirantom
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

class 11 COMPUTER PROJECT solution

The document outlines a series of Java programming tasks that involve string manipulation, including creating acronyms, converting sentences to Pig Latin, reversing words, sorting words by length, and identifying character frequencies. Each task includes sample input and output, along with the corresponding Java code to achieve the desired results. The document serves as a comprehensive guide for students to practice their Java programming skills related to string handling.

Uploaded by

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

COMPUTER PROJECT FOR ANNUAL

Question 1:Write a program in java to input a sentence in uppercase and frame a word by adding the first letter of
each word and also arrange the letters of the new word in an alphabetical order. Display the new word and arranged
word.

Sample Input: MANMOHAN SINGH IS THE PRIME MINISTER OF INDIA


Sample Output: The new word: MSITPMOI
Arranged in alphabetical order: IIMMOPST
import java.util.Scanner;
public class new_first_alpha_alphabetical
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String st=s.nextLine();
st=st.toUpperCase();
st=" "+st;
char ch;
String st1="";
for(int i=0;i<st.length();i++)
{
ch=st.charAt(i);
if(ch==' ')
st1=st1+(st.charAt(++i));

}
System.out.println("original string"+st);
System.out.println("original string"+st1);
char ch1,ch2;
String alpha="";
for(int i=65;i<=90;i++)
{
for(int j=0;j<st1.length();j++)
{
ch1=st1.charAt(j);
if(ch1==(char)i)
{
alpha=alpha+ch1;
}
}
}
System.out.print("String in alphabetical order"+alpha);

}
}

Question 2:Write a program in Java to accept a sentence and display each word of the sentence in Piglatin form.

Sample Input: THE CAPITAL OF INDIA IS NEW DELHI


Sample Output: ETHAY APITALCAY OFAY INDIAAY ISAY EWNAY ELHIDAY
import java.util.*;
public class pig_latin_sentence
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string");
String st=s.nextLine();
st=st.toUpperCase();
StringTokenizer ss=new StringTokenizer(st);
String st1="",st2="";
int len=0,temp=0;
char ch;
while(ss.hasMoreTokens())
{
st1=ss.nextToken();
len=st1.length();
for(int i=0;i<len;i++)//computer science
{
ch=st1.charAt(i);
if(ch=='A'||ch=='E'|| ch=='I'|| ch=='O'||ch=='U')
{
temp=i;//1
break;
}
}
st2=st2+" "+st1.substring(temp,len)+st1.substring(0,temp)+"AY";
}
System.out.println("the original string is"+st);
System.out.println("modified string is"+st2);
}}

Question 3:Write a program in Java to accept a sentence. Display the new sentence after reversing the characters of
each word.

Sample Input: India is my country Sample Output: aidnI si ym yrtnuoc


import java.util.*;

public class reverse_each_word {

public static void main(String[] args)


{
Scanner s=new Scanner(System.in);
System.out.println("enter the string");
String st=s.nextLine();
StringTokenizer ss=new StringTokenizer(st);
String st1="",rev="",st2="";
int len=0;
char ch;
while(ss.hasMoreTokens())//old is gold
{
st1=ss.nextToken();//gold
len=st1.length();
for(int i=0;i<len;i++)
{
ch=st1.charAt(i);
rev=ch+rev;//dlog
}
st2=st2+" "+rev;//dlo si dlog
rev="";
}System.out.println("original string"+st);
System.out.println("reversing each word in string"+st2);

}
}

Question 4: Write a program in Java to accept a string terminated by full stop. Arrange all the words in descending
order of their lengths. Same length of words should be sorted alphabetically. Each word must start with uppercase
and the sentence should be terminated by full stop.

Sample Input: This is the human resource department.


Sample Output: Department Resource Human This Is.
Sample Input: To handle yourself use your head and to handle use your heart.
Sample Output: Yourself Handle Others Heart Head Your Your And Use Use To To.
import java.util.*;
class arrange_len{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String str=s.nextLine();
str=" "+str;
String ss="";
int len = str.length();
char last = str.charAt(len - 1);
if(last!='.')
{
System.out.println("invalid input,string must be terminated by full stop");
return;
}
str=str.substring(0,len-1);
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);//0
if(ch==' ')
ss=ss+" "+Character.toUpperCase(str.charAt(++i));
else
ss=ss+ch;

}
StringTokenizer st = new StringTokenizer(ss);
int count = st.countTokens();
String a[] = new String[count];
for(int i = 0; i < count; i++)
{
a[i] = st.nextToken();
}
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a.length - 1 - i; j++)
{
if(a[j].length() != a[j + 1].length())
{
if(a[j].length() < a[j + 1].length())
{
String temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
else
{
if(a[j].compareTo(a[j + 1]) > 0)
{
String temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
System.out.print(".");
}
}
Question 5: Write a program in Java to accept a String and display the character, which occurs maximum number of
times in the String. Display the frequency of the character also.
import java.util.*;
public class fre_max_char
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String st=s.nextLine();
String st1="";
char ch1,ch2,max_char=0;
int len=st.length();
st=st.toLowerCase();
int count=0, max=0;
for(int i = 0; i <st.length()-1; i++)
{
ch1 = st.charAt(i);
for(int j=0;j<st.length();j++)
{
ch2 = st.charAt(j);
if((ch1==ch2)&&(ch1!=' ') )
{
count++;
}
}
if(count>0)
{
System.out.println(ch1 +" "+count);
if(count>max)
{
max=count;
max_char=ch1;
}
}
count=0;
st=st.replace(ch1,' ');
}
System.out.println("max occurance of character is"+max_char+" "+max);
}}

Question 6:Write a program in Java to accept a sentence and display all the consecutive and repeated present in the
given sentence.

Sample Input: understanding computer science Sample Output: Consecutive Characters: de, rs, st
Repeated Characters: u, n, e, r, s, t, i, c
import java.util.Scanner;
class consecutive_repeated
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String st=s.nextLine();
String st1="";
char ch1,ch2;
int len=st.length();
st=st.toLowerCase();
String consecutive = "";

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


{
ch1 = st.charAt(i);
ch2 = st.charAt(i + 1);
if(ch1-ch2==-1)
consecutive =consecutive+ ch1 + " " + ch2 + " ";
}
System.out.println("Consecutive characters:\n" + consecutive);

for(int i = 0; i <st.length()-1; i++)//science


{
ch1 = st.charAt(i); //e
for(int j=i+1;j<st.length()-1;j++)
{
ch2 = st.charAt(j); //n" "e
if((ch1==ch2)&&(ch1!=' ') )
{

st1=st1+" "+ch1;//ce
st=st.replace(ch1,' ');//n""""
}
}}
System.out.println("repeated characters:\n" + st1);
}
}

Question 7:Write a program in Java to accept a word and arrange all the letters in ascending order. Display the
missing letters in the sequence of the arranged word.
Sample Input: DELHI Arranged word: DEHIL Sample Output: FGJK
import java.util.Scanner;

public class missing_letters


{

public static void main(String[] args)


{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String st=s.next();
st=st.toUpperCase();
char ch;
int len=st.length();
String alpha="";
for(int i=65;i<=90;i++)
{
for(int j=0;j<len;j++)
{
ch=st.charAt(j);
if(ch==(char)i)
{
alpha=alpha+ch;
}
}
}
char ch1=alpha.charAt(0);
char ch2=alpha.charAt(len-1);
int x=(int)ch1;
int y=(int)ch2;
String str="",missing_letters="";
for(int i=x;i<y;i++)
{
str=str+(char)i;
}
for(int i=0;i<len;i++)
{
ch=alpha.charAt(i);
str=str.replace(ch,' ');

}
for(int i=0;i<str.length();i++)
{
ch=str.charAt(i);
if(ch!=' ')
missing_letters=missing_letters+ch;
}

System.out.println("String in alphabetical order"+alpha);


System.out.println("missing letters "+ missing_letters);
}
}
Question 8: One of the systems of calculating word value is Hebrew Numerology, in which corresponding numberic
value of the letters (1 to 26 for A to Z) are joined together. A “Happy Word” is a word whose eventual sum of square
of the digits of its word value is equal to 1. Write a program in Java to input a word and check whether it is a “Happy
Word” or not. The program displays message accordingly.
Sample Input: VAT
Place value of V=22, A=1, T=20

Solution: 22 + 22 + 12 + 22 + 0 = 13 ⇒ 12 + 32 = 10 ⇒
[Hint: A=1, B=2 … , Z=26]

12 + 0 = 1
Sample Output: A Happy Word
import java.util.Scanner;

public class HappyWord


{
static boolean isHappyNumber(long num)
{
long sum = 0;
long n = num;
do {
sum = 0;
while (n != 0) {
int d = (int)(n % 10);
sum += d * d;
n /= 10;
}
n = sum;
} while (sum > 6);

return (sum == 1);


}

public static void main(String args[])


{
Scanner in = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = in.next();
word = word.toUpperCase();
String wordValueStr = "";
int len = word.length();

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


wordValueStr += String.valueOf(word.charAt(i) - 64);
}

long wordValue = Long.parseLong(wordValueStr);


boolean isHappy = isHappyNumber(wordValue);

if (isHappy)
System.out.println("A Happy Word");
else
System.out.println("Not a Happy Word");
}
}

Question 9:Write a program in Java to input a sentence. Convert the sentence to Uppercase. Count and display the
words those have at least a pair of consecutive letters.

Sample Input: Modem is an electronic device.


Sample Output: MODEM IS AN ELECTRONIC DEVICE
MODEM DEVICE
import java.util.*;
class pair_consecutive
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the string ");
String st=s.nextLine();

char ch1,ch2;

st=st.toLowerCase();
String consecutive = "";
StringTokenizer ss=new StringTokenizer(st);

String st1="",st2="";
int len=0,count=0;
char ch;
while(ss.hasMoreTokens())
{
st1=ss.nextToken();
len=st1.length();

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


{
ch1 = st1.charAt(i);
ch2 = st1.charAt(i + 1);
if(ch1-ch2==-1)
{

System.out.println(st1);
}}
}
}}
Write a program to accept a sentence which may be terminated by either ‘.’ or ‘?’ only. The words are to be
separated by a single blank space. Print an error message if the input does not terminate with ‘.’ Or ‘?’. You can
assume that no word in the sentence exceeds 15 characters, so that you can get a proper formatted output. Perform
the following tasks:
Convert the first letter of each word to uppercase
Find the number of vowels and consonants in each word and display them with proper headings along with the
words.
Example 1
Sample Input: Intelligence plus character is education. Sample Output: Intelligence Plus Character Is Education.
Word Vowels Consonants
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
Sample Input: God is great. Sample Output: God Is Great
Word Vowels Consonants
God 1 2

Is 1 1
Great 2 3
Example 3
Sample Input: All the best! Sample Output: Invalid Input
import java.util.Scanner;
public class StringProgram
{
public static void main(String args[])
{
String sen="",wd="",upper="";
char ch=' ',last=' ';
int i=0,len=0,vowelsFrequency=0,consonantsFrequency=0;
Scanner sc=new Scanner(System.in);
System.out.print("String: ");
sen = sc.nextLine();
sen = sen.trim();
len = sen.length();
last = sen.charAt(len - 1);
if(last != '.' && last != '?')
{
System.out.println("Invalid input.");

}
else
{
System.out.println("Word\tVowels\tConsonants");
for(i = 0; i < len; i++)
{
ch = sen.charAt(i);
if(ch == ' ' || ch == '.' || ch == '?')
{
wd=Character.toUpperCase(wd.charAt(0))+wd.substring(1).toLowerCase();

System.out.println(wd+"\t"+vowelsFrequency+"\t"+consonantsFrequency);
wd = "";
vowelsFrequency=0;
consonantsFrequency=0;
}

else
{
wd += ch;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
vowelsFrequency++;
}
else
{
consonantsFrequency++;
}
}
}
}
}
}

You might also like