0% found this document useful (0 votes)
108 views59 pages

Computer Application Project Programs

Uploaded by

Rishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views59 pages

Computer Application Project Programs

Uploaded by

Rishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

THE COMPUTER

APPLICATION
PROJECT

Name : Rishi Saha


Class : 10
Section : B
Roll Number : 44
Registration Number : 8701020

1
Index
Serial Number Programs Teacher's
Remark
1. A program on
method
handling.
2. A program on
method
overloading
3. A program on
constructor
handling
4. A program on
constructor
overloading
5. A program on
single
dimensional
array
6. A program on
bubble sort
array
7. A program on
selection sort

2
array

8. A program on
linear search
array
9. A program on
binary search
array
10. A program on
double
dimensional
array
11. Input a string
and print it in
reverse.
12. Input a string
and count the
total number
of vowels
present in it.

13. Input a string


and print the
first letter of
each word.

3
14. Input two
strings. Verify
whether the
smaller string
is at the end of
the larger
string.
15. Input a string
and print its
first and last
word.

16. Input a string


and print the
frequency of
each
character.

17. Input a string


and print the
frequency of
each character
in a difficult
way.
18. Input a
sentence and
display the
total no. of

4
letters, digits,
uppercase
letters,
lowercase
letters, spaces,
special
characters,
vowels and
consonants are
present in it.

19. Input the


complete name
of a person and
display the
initials and
surname.
20. Input a
sentence and
reverse each
word without
reversing the
sequence of the
words.

5
Q1. A program on method handling.

Example : Reversing the digits.

class ReverseDigits
{
void fnDigits(int N)
{
[Link]("Digits in reverse: ");
while (N > 0) {
int d = N % 10;
[Link](d + " ");
N = N / 10;
}
}
public void main()
{
ReverseDigits obj = new ReverseDigits();
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
[Link](num);
}
}

Variable Description Table:

Variable Data Type Description

N int Input number

6
d int Stores digit
extracted
num int User inputs number

Output:

Enter a number: 1234


Digits in reverse: 4 3 2 1

7
Q2. A program on Method Overloading.

Example: Verifying prime number and factor

import [Link].*;
class VerifyNumber
{
boolean Verify(int n)
{
int count = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
count++;
}
if (count == 2)
return true;
else
return false;
}

boolean Verify(int v, int f)


{
if (v % f == 0)
return true;
else
return false;
}

public void main()

8
{
VerifyNumber obj = new VerifyNumber();
Scanner sc = new Scanner([Link]);
[Link]("Enter a number to check prime: ");
int num = [Link]();
boolean primeResult = [Link](num);
[Link]("Is Prime? " + primeResult);
[Link]("Enter value v: ");
int v = [Link]();
[Link]("Enter factor f: ");
int f = [Link]();
boolean factorResult = [Link](v, f);
[Link]("Is " + f + " a factor of " + v + "? " +
factorResult);
}
}

Variable Description Table :

Variable Data Type Description


n int Number to check
for prime
count int Counts the number
of factors of n
f int Number to check if
it is a factor
v int Number whose
factor is to be
checked
num int User input number

9
for prime check
primeResult boolean Stores result of
prime check
factorResult boolean Stores result of
factor check

Output :

Enter a number to check prime: 17


Is Prime? true
Enter value v: 20
Enter factor f: 5
Is 5 a factor of 20? true

10
Q3. A program by using Constructor.

Example : Payment of salary to the employees

import [Link];
class Pay
{
int basic;
double tax;
double gross;
Pay()
{
basic = 0;
tax = 0.0;
gross = 0.0;
}
void Input()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter basic salary: ");
basic = [Link]();

[Link]("Enter tax amount: ");


tax = [Link]();
}

void Compute()
{
gross = basic - tax;

11
}

void Display()
{
[Link]("Basic Salary: " + basic);
[Link]("Tax: " + tax);
[Link]("Gross Salary: " + gross);
}

public void main()


{
Pay obj = new Pay();
[Link]();
[Link]();
[Link]();
}
}

Variable Description Table :

Variable Data Type Description


basic int Stores the basic
salary

tax double Stores the


calculated gross
salary

gross double Stores the tax


amount to be
deducted

12
Output :

Enter basic salary: 20000


Enter tax amount: 1500
Basic Salary: 20000
Tax: 1500.0
Gross Salary: 18500.0

13
Q4. A program of Constructor Overloading.

Example : Parking System

import [Link];
class Park
{
double EntryFee, Discount, Amount;
int Age;
Park()
{
EntryFee = 0.0;
Discount = 0.0;
Amount = 0.0;
Age = 0;
}
void Input()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter Entry Fee: ");
EntryFee = [Link]();
[Link]("Enter Age: ");
Age = [Link]();
[Link]("Enter Discount (enter 0, discount will be
applied automatically): ");
Discount = [Link]();
}
void Calculate()
{

14
if (Age <= 12)
{
Discount = 45;
}
else if (Age >= 50)
{
Discount = 50;
}
else
{
Discount = 10;
}
Amount = EntryFee - (EntryFee * Discount / 100);
}
void Display()
{
[Link]("\n--- PARK ENTRY DETAILS ---");
[Link]("Entry Fee: " + EntryFee);
[Link]("Age: " + Age);
[Link]("Discount: " + Discount + "%");
[Link]("Amount Payable: " + Amount);
}
public static void main()
{
Park obj = new Park();
[Link]();
[Link]();
[Link]();
}
}

Variable Description Table:

15
Data Member​ Description

EntryFee​ Age​ Discount​ Amount​


Variable Data Type Description
EntryFee double The entry fee of the
park
Age int Age of the visitor

Discount double Discount


percentage based
on age

Amount double Final amount


payable after
discount

Output :

Enter Entry Fee: 150


Enter Age: 10
Enter Discount (enter 0, discount will be applied automatically):
0
--- PARK ENTRY DETAILS ---
Entry Fee: 150.0
Age: 10
Discount: 45.0%
Amount Payable: 82.5

16
Q5. A single dimensional array program.

Example : Adding the values of 2 arrays in one.

import [Link].*;
class Array
{
void main()
{
int A[] = new int[10];
int B[] = new int[10];
int Sum[] = new int [10]; int i ;
Scanner sc = new Scanner ([Link]);
[Link] ("Enter 10 elements of A[] : ");
for(i = 0; i<10; i++)
{
[Link]("Enter value for cell num : "+ i + " :: ");
A[i] = [Link]();
}
[Link] ("Enter 10 elements for B[] : ");
for(i = 0; i<10; i++)
{
[Link]("Enter for cell num : "+ i + " :: ");
B[i] = [Link]();
}
for(i = 0; i<10; i++)
{
Sum[i] = A[i] + B[i] ;
}

17
[Link]("\n Elements of A[ ] : ");
for(i = 0; i<10; i++)
{
[Link](" .. " + A[i] );
}
[Link]("\n Elements of B[ ] : ");
for(i = 0; i<10; i++)
{
[Link](" .. " + B[i] );
}
[Link]("\n Sum of Elements of A[ ] & B[ ] : ");
for(i = 0; i<10; i++)
{
[Link](" .. " + Sum[i] );
}
}
}

Variable Description Table:

Variable Data Type Description


A[] int Integer array of
size 10 used to
store the first list of
numbers entered by
the user.

B[] int Integer array of


size 10 used to
store the second list
of numbers

18
Sum[] int Integer array of
size 10 used to
store the
element-wise sum
of A[] and B[].

i int Loop counter used


to access array
indices from 0 to 9.

OUTPUT :

Enter 10 elements of A[] :


Enter value for cell num : 0 :: 15
Enter value for cell num : 1 :: 64
Enter value for cell num : 2 :: 97
Enter value for cell num : 3 :: 82
Enter value for cell num : 4 :: 56
Enter value for cell num : 5 :: 61
Enter value for cell num : 6 :: 79
Enter value for cell num : 7 :: 87
Enter value for cell num : 8 :: 23
Enter value for cell num : 9 :: 30
Enter 10 elements for B[] :
Enter for cell num : 0 :: 5
Enter for cell num : 1 :: 12
Enter for cell num : 2 :: 3
Enter for cell num : 3 :: 9
Enter for cell num : 4 :: 10
Enter for cell num : 5 :: 9
Enter for cell num : 6 :: 3
Enter for cell num : 7 :: 6

19
Enter for cell num : 8 :: 1
Enter for cell num : 9 :: 20
Elements of A [ ] :
.. 15 .. 64 .. 97 .. 82 .. 56 .. 61 .. 79 .. 87 .. 23 .. 30
Elements of B [ ] :
.. 5 .. 12 .. 3 .. 9 .. 10 .. 9 .. 3 .. 6 .. 1 .. 20
Sum of Elements of A[ ] & B[ ] :
.. 20 .. 76 .. 100 .. 91 .. 66 .. 70 .. 82 .. 93 .. 24 .. 50

20
Q6. A bubble sort array program.

Example : Sorting the Array.

import [Link].*;

class BubbleSort
{
void main()
{
int i, j, temp;
int A[] = new int[10];
Scanner sc = new Scanner([Link]);
[Link]("Enter 10 numbers:");
for(i = 0; i < 10; i++)
{
[Link]("Enter value for position " + i + " : ");
A[i] = [Link]();
}
for(i = 0; i < 10; i++)
{
for(j = 0; j < 9 - i; j++)
{
if(A[j] > A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}

21
}
[Link]("\nSorted Array:");
for(i = 0; i < 10; i++)
{
[Link](" " + A[i]);
}
}
}

Variable Description Table:

Variable Data Type Description


A[]​ int Array of 10 integers
entered by the user
which will be
sorted.

i int Outer loop counter


used for controlling
the number of
passes.

j int Inner loop counter


used for comparing
adjacent elements.

temp int Temporary variable


used for swapping
two array elements.

22
Output :

Enter 10 numbers:
Enter value for position 0 : 12
Enter value for position 1:7
Enter value for position 2 : 19
Enter value for position 3:3
Enter value for position 4 : 15
Enter value for position 5:8
Enter value for position 6:2
Enter value for position 7 : 10
Enter value for position 8:6
Enter value for position 9:1

Sorted Array:
1 2 3 6 7 8 10 12 15 19

23
Q7. A selection sort array program.

Example : Sorting the Array.

import [Link].*;

class SelectionSort
{
void main()
{
int i, j, min, temp;
int A[] = new int[10];
Scanner sc = new Scanner([Link]);
[Link]("Enter 10 numbers:");
for(i = 0; i < 10; i++)
{
[Link]("Enter value for position " + i + " : ");
A[i] = [Link]();
}
for(i = 0; i < 10 - 1; i++)
{
min = i;
for(j = i + 1; j < 10; j++)
{
if(A[j] < A[min])
{
min = j;
}
}
temp = A[i];

24
A[i] = A[min];
A[min] = temp;
}
[Link]("\nSorted Array:");
for(i = 0; i < 10; i++)
{
[Link](" " + A[i]);
}
}
}

Variable Description Table:

Variable Data Type Description


A[] int Array of 10 integers
entered by the user,
to be sorted.

i int Outer loop counter


to track each index
position

j int Inner loop counter


to find the
minimum element.

min int Stores the index of


the smallest
element found in
the unsorted

25
temp int Temporary variable
used for swapping
two elements.

Output :

Enter 10 numbers:
Enter value for position 0 : 15
Enter value for position 1:3
Enter value for position 2 : 20
Enter value for position 3:1
Enter value for position 4:8
Enter value for position 5 : 12
Enter value for position 6:6
Enter value for position 7 : 18
Enter value for position 8 : 10
Enter value for position 9:2

Sorted Array:
1 2 3 6 8 10 12 15 18 20

26
Q8. A linear search program on a single dimensional
array.

Example : Searching for a number in the array.

import [Link].*;

class LinearSearch
{
void main()
{
int i, key, pos = -1;
int A[] = new int[10];
Scanner sc = new Scanner([Link]);
[Link]("Enter 10 numbers:");
for(i = 0; i < 10; i++)
{
[Link]("Enter value for position " + i + " : ");
A[i] = [Link]();
}
[Link]("\nEnter the number to search: ");
key = [Link]();
for(i = 0; i < 10; i++)
{
if(A[i] == key)
{
pos = i;
break;
}

27
}
if(pos == -1)
[Link]("Element not found.");
else
[Link]("Element found at position: " + pos);
}
}

Variable Description Table:

Variable Data Type Description


A[] int Array of 10 integers
entered by the user.

i int Loop counter to


traverse the array.

key int The number to be


searched in the
array.

pos int Stores index


position where the
element is found (-1
means not found).

Output :

Enter 10 numbers:
Enter value for position 0 : 5

28
Enter value for position 1:9
Enter value for position 2:3
Enter value for position 3:7
Enter value for position 4:2
Enter value for position 5:8
Enter value for position 6:1
Enter value for position 7:6
Enter value for position 8:0
Enter value for position 9:4

Enter the number to search: 7


Element found at position: 3

29
Q9. A binary search program on a single dimensional array.

Example : Searching for a number in the array.

import [Link].*;
class BinarySearch
{
void main()
{
int i, key, low, high, mid, pos = -1;
int A[] = new int[10];
Scanner sc = new Scanner([Link]);
[Link]("Enter 10 numbers in ascending
order:");
for(i = 0; i < 10; i++)
{
[Link]("Enter value for position " + i + " : ");
A[i] = [Link]();
}
[Link]("\nEnter the number to search: ");
key = [Link]();
low = 0;
high = 9;
while(low <= high)
{
mid = (low + high) / 2;
if(A[mid] == key)
pos = mid;
break;

30
else if(key > A[mid])
low = mid + 1;
else
high = mid - 1;

}
if(pos == -1)
[Link]("Element not found.");
else
[Link]("Element found at position: " + pos);
}
}

Variable Description Table:

Variable Data Type Description


A[] int Sorted array of 10
integers entered by
the user.

i int Loop counter used


to input array
elements.

key int The number to be


searched.

low int Lower bound index


of the search

high int Upper bound index


of the search range.

31
mid int Middle index used
to compare the key.

pos int Stores the index


where the key is
found (-1 means not
found).

Output :

Enter 10 numbers in ascending order:


Enter value for position 0 : 1
Enter value for position 1 : 4
Enter value for position 2 : 7
Enter value for position 3 : 9
Enter value for position 4 : 12
Enter value for position 5 : 15
Enter value for position 6 : 18
Enter value for position 7 : 21
Enter value for position 8 : 25
Enter value for position 9 : 30

Enter the number to search: 20


Element not found.

32
Q10. A Double dimensional array program.

Example : It is like a matrix.

import [Link].*;
class TwoDArraySimple
{
void main()
{
int A[][] = new int[3][3];
int i, j, sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter 9 values for the 3x3 matrix:");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
[Link]("Enter value for [" + i + "][" + j + "]
: ");
A[i][j] = [Link]();
}
}
[Link]("\nMatrix:");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
[Link](A[i][j] + " ");

33
sum += A[i][j];
}
[Link]();
}
[Link]("\nSum of all elements = " + sum);
}
}

Variable Description Table:

Variable Data Type Description


A[][] int 3×3 matrix used to
store user-entered
values.

i int Controls the row


index in loops.

j int Controls the column


index in loops.

sum int Stores the total of


all elements in the

Output :

Enter 9 values for the 3x3 matrix:


Enter value for [0][0] : 2
Enter value for [0][1] : 4
Enter value for [0][2] : 6

34
Enter value for [1][0] : 1
Enter value for [1][1] : 3
Enter value for [1][2] : 5
Enter value for [2][0] : 7
Enter value for [2][1] : 8
Enter value for [2][2] : 9

Matrix:
246
135
789

Sum of all elements = 45

35
Q11. String input program.

Example : Reversing the String

import [Link].*;
class ReverseString
{
void main()
{
Scanner sc = new Scanner([Link]);
String s, rev = " ";
int i;
[Link]("Enter a string: ");
s = [Link]();
for(i = [Link]() - 1; i >= 0; i--)
{
rev = rev + [Link](i);
}
[Link]("Reversed string: " + rev);
}
}

Variable Description Table:

Variable Data Type Description


s String Stores the input

36
string entered by
the user.
rev String Stores the reversed
string formed in the
loop.

i int Loop counter used


to traverse the
string backward.

Output :

Enter a string: India


Reversed string: aidnI

37
Q12. String input program.

Example : Count the number of vowels present in the String.

import [Link];
class CountVowels
{
public void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String s = [Link]();
int count = 0;
char ch;
for (int i = 0; i < [Link](); i++)
{
ch = [Link]([Link](i));
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
count++;
}
}
[Link]("Number of vowels: " + count);
}
}

Variable Description Table:

38
Variable Data Type Description
s String Stores the input
string.

count int Stores the number


of vowels.

i int Loop counter.


ch char Stores each
character while
checking.

Output :

Enter a string: computer


Number of vowels: 3

39
Q13. String input program.

Example : Printing the first letter of each word of the String.

import [Link].*;
class FirstLetter
{
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]();
s = [Link]() + " ";
char ch;
[Link]("First letters: ");
[Link]([Link]([Link](0)));
for (int i = 1; i < [Link](); i++)
{
ch = [Link](i);
if (ch == ' ')
{
[Link]([Link]([Link](i
+ 1)));
}
}
}

40
}

Variable Description Table:

Variable Data Type Description


s String Stores the input
sentence

i int Loop counter

ch char Stores character


while scanning

Output :

Enter a sentence: My Name Is Rishi Saha


First letters: MNIRS

41
Q14. String input program.

Example : Checking whether the smaller string is at the back of


the larger string.

import [Link].*;
class EndCheck
{
String s1, s2;
void input()
{
Scanner sc = new Scanner([Link]);
s1 = [Link]();
s2 = [Link]();
}
void check()
{
int l1 = [Link]();
int l2 = [Link]();
if(l1 > l2)
{
if([Link](s2))
[Link]("Yes");
else
[Link]("No");
}
else

42
{
if([Link](s1))
[Link]("Yes");
else
[Link]("No");
}
}
void main()
{
EndCheck ob = new EndCheck();
[Link]();
[Link]();
}
}

Variable Description Table:

Variable Data Type Description


s1 String Stores the first
input string

s2 String Stores the second


input string

l1 int Length of s1

l2 int Length of s2

43
Output :

HelloWorld
World
Yes

Q15. String input program.

Example : Printing the first and last word of the String.

import [Link].*;

class FirstLastWord
{
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]().trim();
int firstSpace = [Link](' ');
int lastSpace = [Link](' ');
String first = [Link](0, firstSpace);
String last = [Link](lastSpace + 1);
[Link]("First word: " + first);
[Link]("Last word: " + last);
}
}

Variable Description Table:

Variable Data Type Description

44
s String Stores the input
sentence.
firstSpace int Position of first
space.
lastSpace int Position of last
space.
first String Stores the first
word.

last String Stores the last


word.

Output :

Enter a sentence: my name is Rishi Saha


First word: my
Last word: Saha

45
Q16. String input program.

Example : Calculating the Frequency of the characters of the


String.

import [Link].*;

class CharFrequency
{
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String s = [Link]();
s = [Link](" ", "");
int len = [Link]();
[Link]("Character Frequency:");
for (int i = 0; i < len; i++)
{
char ch = [Link](i);
int count = 0;
boolean seen = false;
for (int k = 0; k < i; k++)
{
if ([Link](k) == ch)
{

46
seen = true;
break;
}
}
if (seen) continue;
for (int j = 0; j < len; j++)
{
if ([Link](j) == ch)
{
count++;
}
}
[Link](ch + " : " + count);
}
}
}

Variable Description Table:

Variable Data Type Description


s String Stores the input
string

len int Length of the


processed string

i int Loop counters

j int Loop counters

k int Loop counters

47
ch char Current character

count int Stores frequency of


a character

seen boolean Checks if character


already counted

Output :

Enter a string: banana


Character Frequency:
b:1
a:3
n:2

48
Q17. String input program.

Example : Calculating the Frequency of the characters of the


String in a difficult way.

import [Link].*;
class CharFreqHard
{
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String s = [Link]().replace(" ", "");
int len = [Link]();
char data[][] = new char[len][2];
int freq[] = new int[len];
int r = 0;
for (int i = 0; i < len; i++)
{
char ch = [Link](i);
boolean exists = false;
for (int k = 0; k < r; k++) {
if (data[k][0] == ch)
{
freq[k]++;
exists = true;

49
break;
}
}
if (!exists)
{
data[r][0] = ch;
freq[r] = 1;
r++;
}
}
[Link]("Character Frequency:");
for (int i = 0; i < r; i++)
{
[Link](data[i][0] + " : " + freq[i]);
}
}
}

Variable Description Table:

Variable Data Type Description


s String Stores input string

len int Length of string

data char Stores characters

freq int Stores frequency of


characters

r Int Row counter for


unique characters

50
i int Loop counters

k int Loop counters

ch char Current character

exists boolean True if character


already stored

Output :

Enter a string: success


Character Frequency:
s:3
u:1
c:2
e:1

51
Q18. String input program.

Example : Displaying the total no. of letters, digits, uppercase


letters, lowercase letters, spaces, special characters, vowels and
consonants are present in the String.

import [Link].*;
class SentenceAnalysis
{
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]();
int letters = 0, digits = 0, upper = 0, lower = 0, spaces = 0,
special = 0, vowels = 0,
consonants = 0
char ch;
for (int i = 0; i < [Link](); i++)
{
ch = [Link](i);
if ([Link](ch))
{
letters++;
if ("AEIOUaeiou".indexOf(ch) != -1)
vowels++;
else
consonants++;

52
if ([Link](ch))
upper++;
else
lower++;
}
else if ([Link](ch))
digits++;
else if (ch == ' ')
spaces++;
else
special++;
}

[Link]("Letters: " + letters);


[Link]("Digits: " + digits);
[Link]("Uppercase letters: " + upper);
[Link]("Lowercase letters: " + lower);
[Link]("Spaces: " + spaces);
[Link]("Special characters: " + special);
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
}
}

Variable Description Table:

Variable Data Type Description


s String Stores the input
sentence

53
letters int Counts total
alphabetic letters

digits int Counts numeric


digits

upper int Counts uppercase


letters

lower int Counts lowercase


letters

spaces int Counts blank


spaces

special int Counts special


characters

vowels int Counts vowels

constants int Counts consonants

ch char Stores each


character of the
string

i int Loop counter

Output :

Enter a sentence: Hello India 2025!


Letters: 10
Digits: 4
Uppercase letters: 2
Lowercase letters: 8

54
Spaces: 2
Special characters: 1
Vowels: 5
Consonants: 5

Q19. String input program.

Example : Displaying the initials and surname of the person.

import [Link].*;
class InitialSurname
{
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter full name: ");
String s = [Link]().trim();
s = [Link]("\\s+", " ");
int lastSpace = [Link](' ');
String surname = [Link](lastSpace + 1);
String initials = "";
initials += [Link]([Link](0)) + " ";
for (int i = 1; i < lastSpace; i++)
{
if ([Link](i) == ' ')
initials += [Link]([Link](i + 1))
+ " ";
}
[Link](initials + surname);
}
}

Variable Description Table:

55
Variable Data Type Description
s String Stores full name

lastSpace int Position of last


space in the name

surname String Extracted surname

initials String Stores initials

i int Loop counter

Output :

Enter full name: Mohandas Karamchand Gandhi


M K Gandhi

56
Q20. String input program.

Example : Reversing the word without Reversing the sentence.

import [Link].*;
class ReverseEachWord
{
void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence: ");
String s = [Link]().trim() + " ";
String word = "", rev = "";
for (int i = 0; i < [Link](); i++)
{
char ch = [Link](i);
if (ch != ' ')
{
word = word + ch;
}
else
{
rev = "";
for (int j = [Link]() - 1; j >= 0; j--)
{
rev = rev + [Link](j);
}
[Link](rev + " ");

57
word = "";
}
}
}
}

Variable Description Table:

Variable Data Type Description


s String Stores the sentence
with an added
space

word String Stores the current


word being formed

rev String Stores the reversed


form of the current
word

i int Loop counters

j int Loop counters

ch char Stores each


character

Output :

Enter a sentence: COMPUTER IS FUN


RETUPMOC SI NUF

58
59

Common questions

Powered by AI

Selection sort differs from bubble sort in that it selects the smallest unsorted element and swaps it with the first unsorted position, progressively building the sorted section of the array. While bubble sort repeatedly swaps adjacent elements, selection sort reduces the number of swaps by performing one swap per pass. Although both have O(n^2) time complexity, selection sort performs fewer swaps, potentially making it more efficient in scenarios where swapping is costly .

The 'End Check' program compares the lengths of two strings to determine the shorter and longer one. It uses the `endsWith` method to check if the longer string terminates with the shorter string, effectively determining if one string is a suffix of the other through a straightforward boolean comparison .

The two-dimensional array program showcases the initialization and traversal of a matrix, a fundamental matrix operation. It exemplifies the summation of matrix elements, where each element in the 3x3 matrix is iterated, and their collective sum is computed, demonstrating an operation to gather aggregate data from the matrix .

The 'CharFrequency' program employs a nested loop design, where the outer loop traverses each character ensuring it's counted only once by checking if it has been seen previously. This check eliminates redundancy by preventing recounting of characters, while a subsequent loop counts the occurrences of each unaddressed character in the remainder of the string, ensuring accurate frequency analysis .

The 'Reverse Each Word' program iterates through the sentence character by character, building words until a space is found. Upon identifying a space delimiting the end of a word, it reverses the current word and prints it, then resets the word builder for the next word. This approach allows each word to be individually reversed while maintaining sentence order .

The 'Count Vowels' program iterates through each character of the string, normalizing it to lowercase to ensure case insensitivity, and checks if each character matches any vowel ('a', 'e', 'i', 'o', 'u'). By counting the matches across the iteration, it efficiently tallies the total number of vowels present in the string .

Binary search requires the data to be sorted because it repeatedly divides the search interval in half. Without sorting, the assumptions about the sorted order used to eliminate half of the remaining elements in the search space are invalid, making the search process incorrect as it relies on misplaced assumptions about element positioning .

The bubble sort algorithm sorts an array by iteratively scanning the list, comparing adjacent elements, and swapping them if they are in the wrong order (i.e., the first element is larger than the second). This process is repeated for each element, excluding the last sorted elements, until no more swaps are needed. This results in the largest unsorted number being 'bubbled' to its correct position at the end of the array in each pass .

The 'InitialSurname' program identifies the last space in the string to separate the surname. It extracts and capitalizes the first character as the initial, and then iterates through the string to capture and capitalize subsequent initials found after spaces until the last space, pairing these initials with the surname for output .

The linear search algorithm sequentially checks each element of the array from start to end until it finds the element matching the key. If a match is found, the algorithm returns the position of the element; otherwise, it completes after inspecting each element and confirms the absence of the key element by returning -1 .

You might also like