0% found this document useful (0 votes)
10 views5 pages

Java Programs for String and Array Manipulations

The document contains a series of programming questions and examples primarily focused on Java. It includes tasks such as transforming strings, checking for unique elements in arrays, generating Pascal's Triangle, and determining if a number is a Smith number. Each question is accompanied by code snippets and explanations of the expected output.
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)
10 views5 pages

Java Programs for String and Array Manipulations

The document contains a series of programming questions and examples primarily focused on Java. It includes tasks such as transforming strings, checking for unique elements in arrays, generating Pascal's Triangle, and determining if a number is a Smith number. Each question is accompanied by code snippets and explanations of the expected output.
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

Question 21: Write a program that takes a sentence as if ("aeiouAEIOU".indexOf(c)!

=-1)
input and performs the following transformations: c=(char)(c-1);
1. Convert the entire sentence to uppercase. }
2. Replace each vowel in the sentence with the next }
vowel in the sequence, in a circular manner: ○ 'A' → }
'E', 'E' → 'I', 'I' → 'O', 'O' → 'U', 'U' → 'A' str2=str2+c;
3. Replace each consonant with the previous }
consonant in the English alphabet, also in uppercase [Link]("Modify String "+str2);
and circular: ○ 'B' → 'Z', 'C' → 'B', 'D' → 'C', ..., 'Z' → 'Y' }
4. Digits, Spaces and punctuation should remain }
unchanged. Question 20: Write a program that takes a word as
Example: Input: "Back to school" input and performs the following string operations:
Output: "ZEBJ SU RBGUUK" 1. Remove any duplicate characters from the word.
import [Link].*; ○ Example: If the input word is "programming", the
class pro1 result should be "progamin", as duplicate letters are
{ removed.
public static void main (String args[]) import [Link].*;
{ class pro1
Scanner sc=new Scanner([Link]); {
String str,str2=""; public static void main (String args[])
int i; {
[Link]("Enter a String"); Scanner sc=new Scanner([Link]);
str=[Link](); String w1,w2="";
str=[Link](); int i;
for(i=0;i<[Link]();i++) [Link]("Enter a Word");
{ w1=[Link]();
char c=[Link](i); for(i=0;i<[Link]();i++)
if ([Link](c)==true) {
{ char c=[Link](i);
if(c=='A') if ([Link](c)==-1)
c='E'; {
else if(c=='E') w2=w2+c;
c='I'; }
else if(c=='I') }
c='O'; [Link]("Modify Word \n"+w2);
else if(c=='O') }
c='U'; }
else if(c=='U')
c='A'; 2. Find the longest substring without repeating
else characters.
{ ○ Example: If the input word is "abcabcbb", the result
if(c=='B') can be "abc", "bca", or "cab", as all are valid substrings
c='Z'; without repeating characters.
else import [Link].*;
{ class pro1
c=(char)(c-1); {
public static void main (String args[]) {9, 10, 11,12},
{ {13,14,15,16}
Scanner sc=new Scanner([Link]); };
String w1,w2="";
int i,j; if ([Link] == 0)
[Link]("Enter a Word"); return;
w1=[Link](); int top = 0;
for(i=0;i<[Link]();i++) int bottom = [Link] - 1;
{ int left = 0;
for(j=i;j<[Link]();j++) int right = matrix[0].length - 1;
{
char c=[Link](j); while (top <= bottom && left <= right)
if ([Link](c)==-1) {
{
w2=w2+c; for (int i = left; i <= right; i++)
} {
else [Link](matrix[top][i] + " ");
{ }
[Link](w2+ " "); top++;
w2="";
break;
} for (int i = top; i <= bottom; i++) {
} [Link](matrix[i][right] + " ");
} }
} right--;
}
Question 15: Write a Java prog Write a Java program to
traverse a given 2D matrix in spiral order (clockwise). if (top <= bottom) {
The program should print the elements of the matrix for (int i = right; i >= left; i--) {
starting from the top-left corner and moving in a [Link](matrix[bottom][i] + " ");
spiral: left to right, top to bottom, right to left, and }
bottom to top. Input Matrix bottom--;
1 2 3 4 }
5 6 7 8
9 10 11 12
13 14 15 16 if (left <= right) {
Output 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 for (int i = bottom; i >= top; i--) {
[Link](matrix[i][left] + " ");
public class SpiralMatrix }
{ left++;
public static void main(String[] args) }
{ }
int[][] matrix = }
{ }
{1, 2, 3,4}, Question: 11 Write a program to accept a number n
{5, 6, 7,8}, from the user and print Pascal’s Triangle with n lines
using a single-dimensional array. Each line of the Enter the element to insert: 6
triangle should be generated by updating and printing Output Array after insertion: 1 3 5 6 7
the same array, using the rule that each number is the import [Link];
sum of the two numbers above it in the previous row. public class PascalTriangle
Example: If n = 6, the output should be: {
1 public static void main(String[] args)
1 1 {
1 2 1 Scanner sc = new Scanner([Link]);
1 3 3 1 int n,i,s,j,t;
1 4 6 4 1 [Link]("Enter size of array");
1 5 10 10 5 1 n=[Link]();
import [Link]; int x[]=new int[n];
public class PascalTriangle [Link]("Enter number of elements into
{ an array");
public static void main(String[] args) for(i=0;i<n;i++)
{ {
Scanner sc = new Scanner([Link]); x[i]=[Link]();
[Link]("Enter the number of rows for }
Pascal's Triangle: "); [Link]("Enter an elements you want to
int n = [Link](); insert");
for (int i = 0; i < n; i++) s=[Link]();
{ for(i=0;i<n;i++)
for (int j = 0; j < n - i - 1; j++) {
{ if(x[i]>s)
[Link](" "); break;
} }
int x = 1; for(j=i;j<n;j++)
for (int j = 0; j <= i; j++) {
{ t=x[j];
[Link](x+" "); x[j]=s;
x = x * (i - j) / (j + 1); s=t;
} }
[Link](); [Link]("Modify array elements");
} for(i=0;i<n;i++)
} {
} [Link](x[i]+" ");
Question 12: Write a Java program that accepts a }
sorted array and an element to insert, then inserts the }
element into the appropriate position in the sorted }
array while maintaining the sorted order. The program Question 10: Write a program that takes ‘n’ integers as
should handle the shifting of elements to make space input to form an array and checks whether all the
for the new element and remove the last element to elements in the array are unique or if there are any
maintain the original array size. After insertion, print duplicates. For example, if the input array is [1, 2, 3, 4,
the updated array. 5], the program should output that all elements are
Example: Input Enter the size of the array: 5 unique. If the input array is [3, 7, 2, 3, 8, 9], the
Enter 5 sorted elements: 1 3 5 7 9 program should indicate that there are duplicates.
import [Link]; fewer digits, the remaining digits of the longer number
public class PascalTriangle should be appended at the end of the merged result.
{ For example:
public static void main(String[] args) ● If the numbers are 1234 and 567, the merged result
{ should be 1526374.
Scanner sc = new Scanner([Link]); ● If the numbers are 12 and 34567, the merged result
int n,i,c,j,f=0; should be 1324567.
[Link]("Enter size of array"); import [Link];
n=[Link](); public class PascalTriangle
int x[]=new int[n]; {
[Link]("Enter number of elements into public static void main(String[] args)
an array"); {
for(i=0;i<n;i++) Scanner sc = new Scanner([Link]);
{ int m,n,i;
x[i]=[Link](); String s1,s2,w="";
} [Link]("Enter two positive integer");
for(i=0;i<n;i++) m=[Link]();
{ s1=[Link](m);
c=0; n=[Link]();
for(j=0;j<n;j++) s2=[Link](n);
{ int
if(x[j]==x[i]) h=[Link]()>[Link]()?[Link]():[Link]();
c++; for(i=0;i<h;i++)
} {
if(c>1) if (i<[Link]())
{ w=w+[Link](i);
f=1; if (i<[Link]())
break; w=w+[Link](i);
} }
} [Link]("New Number"+w);
if(f==0) }
{ }
[Link]("all the elements in the array Question 7: Write a program to check whether a given
are unique "); number is a Smith number or not using user-defined
} methods. A Smith number is a composite number
else whose sum of digits is equal to the sum of digits of its
{ prime factors (excluding 1 and the number itself). Your
[Link]("all the elements in the array program should include the following user-defined
are Not unique "); methods:
} 1. sum_of_digits(n) – to return the sum of digits of a
number.
} 2. prime_factors(n) – to return a list of prime factors of
} the number using prime factorization.
Question 6: Write a Java program that takes two 3. is_smith_number(n) – to check whether the given
positive integers as input and merges them in an number is a Smith number using the above methods.
alternating fashion, digit by digit. If one number has
Prompt the user to input a number and display int n;
whether it is a Smith number. Example: Consider the [Link]("Enter a number");
number 666. n=[Link]();
● Sum of digits = 6 + 6 + 6 = 18 is_smith_number(n);
● Prime factorization = 2 × 3 × 3 × 37 }
● Sum of digits of prime factors = 2 + 3 + 3 + (3 + 7) = }
18
● Since both sums are equal, 666 is a Smith number
import [Link];
class xyz
{
int sum(int x)
{
int i,d,s=0;
for(i=x;i>0;i=i/10)
{
d=i%10;
s=s+d;
}
return s;
}
int prime_factor(int n)
{
int i=2,s=0;
while(n>1)
{
if(n%i==0)
{
s=s+sum(i);
n=n/i;
}
else
i++;
}
return s;
}
void is_smith_number(int n)
{
if(sum(n)==prime_factor(n))
[Link]("Smith Number");
else
[Link]("Not Smith Number");
}
public void main(String[] args)
{
Scanner sc = new Scanner([Link]);

You might also like