Java Coding
Java Coding
CODE 1:
import [Link];
public class Reverse {
OUTPUT:
import [Link].*;
public class Reverse2 {
OTUPUT 2:
Enter string to reverse:
Reverse
esreveR
2. Write a program to print First non-repeated
character from given String?
Ex: Software Services Output: o
CODE:
import [Link];
for(int i=0;i<[Link]();i++) {
int count=0;
if([Link](i)!=' ') {
for(int j=0;j<[Link]();j++) {
if([Link](i)==[Link](j) && i!=j) {
count++;
break;
}
}
if(count==0) {
result += [Link](i);
break;
}
}
}
return result;
}
[Link]("Answer is:
"+firstNonRepeatingNumber(str));
[Link]();
OUTPUT:
Enter String:
Software Services
Answer is: o
CODE:
import [Link];
for(int i=[Link]()-1;i>0;i--) {
int count=0;
if([Link](i)!=' ') {
for(int j=0;j<[Link]();j++) {
if([Link](i)==[Link](j) && i!=j) {
count++;
break;
}
}
if(count==0) {
result += [Link](i);
break;
}
}
}
return result;
}
[Link]("Answer is:
"+lastNonRepeatingNumber(str));
[Link]();
OUTPUT:
Enter String:
Software services
Answer is: c
4. Write a program to remove the duplicate
characters from the given String?
Ex: banaans Output: bans
CODE:
import [Link];
for(int i=0;i<[Link]();i++) {
int count=0;
if([Link](i)!=' ') {
for(int j=i+1;j<[Link]();j++) {
if([Link](i)==[Link](j) &&(i!=j)) {
count++;
}
}
if(count==0) {
result += [Link](i);
}
}
}
return result;
}
[Link](removeDuplicateChar(str));
[Link]();
}
}
OUTPUT:
Enter String:
banaans
bans
CODE:
import [Link];
for(int i=0;i<[Link]();i++) {
int count = 1;
if([Link](i)!=' ') {
for(int j=i+1;j<[Link]();j++) {
if([Link](i)==[Link](j) && i!=j) {
count++;
}
}
if(count==1) {
arr[c++]=[Link](i);
}
}
}
for(int i=0;i<[Link];i++) {
int count2=0;
if((arr[i]>='a' && arr[i]<='z') || (arr[i]>='A' &&
arr[i]<='Z')) {
for(int j=0;j<[Link]();j++) {
if(arr[i]==[Link](j)) {
count2++;
}
}
result += arr[i]+" : "+count2+"\n";
}
}
return result;
}
OUTPUT:
Enter String:
apple
a : 1
p : 2
l : 1
e : 1
import [Link];
for(int i=0;i<[Link]();i++) {
int count=0;
if([Link](i)!=' ') {
for(int j=i+1;j<[Link]();j++) {
if([Link](i)==[Link](j) &&(i!=j)) {
count++;
}
}
if(count==1) {
result += [Link](i)+" ";
}
}
}
return result;
}
[Link](printDuplicateChar(str));
[Link]();
}
OUTPUT:
Enter String:
Programming
r g m
7. write a program to print all duplicate character
and their count form the given String?
Ex: Programming Output: g: 2
r: 2
m: 2
CODE:
import [Link];
for(int i=0;i<[Link]();i++) {
int count=1;
if([Link](i)!=' ') {
for(int j=i+1;j<[Link]();j++) {
if([Link](i)==[Link](j) &&(i!=j)) {
count++;
}
}
if(count>1) {
result += [Link](i)+" : "+count+"\n";
}
}
}
return result;
}
[Link](printDuplicateCharAndCount(str));
[Link]();
OUTPUT:
Enter String:
Programming
r : 2
g : 2
m : 2
CODE:
import [Link];
int c=0;
int max=0;
for(int i=0;i<[Link]();i++) {
int count = 1;
if([Link](i)!=' ') {
for(int j=i+1;j<[Link]();j++) {
if([Link](i)==[Link](j) && i!=j) {
count++;
}
}
if(max<count) {
max=count;
result+=[Link](i);
}
}
}
return result;
}
[Link](countMaxOccuranceOfChar(str));
[Link]();
}
}
OUTPUT:
Enter String:
aaaaabbcddddd
a
9. Write a program to remove the given Character
from the given String?
Ex: nacre Software
Remove character: a
Output: ncre Softwre
CODE:
import [Link];
[Link](removeChar(str,ch));
[Link]();
}
}
OUTPUT:
Enter String:
nacre Software
Enter char to remove:
a
ncre Softwre
CODE:
import [Link];
[Link](checkDigit(str));
[Link]();
}
OUTPUT:
Enter String:
nacre123
Given String Contains Digits
CODE:
import [Link];
OUTPUT:
Enter String:
nacre123@#
nacre123
CODE:
import [Link].*;
public class CheckVowel {
OUTPUT:
Enter String:
nacre123
It Contains Vowels
CODE:
import [Link];
for(int i=0;i<[Link]();i++) {
if([Link](i)!=' ') {
if(([Link](i)>= 'A' && [Link](i)<='Z') ||
([Link](i)>= 'a' && [Link](i)<='z')) {
character++;
}
else if([Link](i)>='0' && [Link](i)<='9'){
number++;
}
else {
special++;
}
}
}
[Link](numberOfCharDigitAndSpecial(str));
[Link]();
}
OUTPUT:
Enter String:
Nacre@123%
Character : 5
Digit : 3
Special : 2
14. Write a program to count the Capital letters
and Small letters from the given String?
Ex: Nacre Software
Output: Capital Characters are 2
Small Characters are 11
CODE:
import [Link];
[Link](numberOfCapAndSmall(str));
[Link]();
}
}
OUTPUT:
Enter String:
Nacre Software
Capital: 2
Small: 11
CODE:
import [Link].*;
public class CountConsAndVowel {
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter String: ");
String str=[Link]();
[Link](countVowelsAndConsonent(str));
[Link]();
}
OUTPUT:
Enter String:
Nacre
Vowels : 2
Consonent: 3
CODE:
import [Link];
for(int i=0;i<len;i++) {
if([Link](i)!=' ') {
if(([Link](i)>= 'A' && [Link](i)<='Z')
|| ([Link](i)>= 'a' && [Link](i)<='z')) {
character++;
}
else if([Link](i)>='0' &&
[Link](i)<='9'){
number++;
}
else {
special++;
}
}
}
float c=(character/len)*100;
float d=(number/len)*100;
float s=(special/len)*100;
[Link](percentageOfCharDigitAndSpecial(str));
[Link]();
}
OUTPUT:
Enter String:
Nacre@123%
Character : 50.00%
Digit : 30.00%
Special : 20.00%
17. Write a program to find the percentages of the
Consonants and vowels from the given String?
CODE:
import [Link].*;
int cons=0,vowel=0;
float len = [Link]();
for(int i=0;i<[Link]();i++) {
if([Link](i)!=' ')
{
if([Link](i)=='a' || [Link](i)=='e' ||
[Link](i)=='i' || [Link](i)=='o' || [Link](i)=='u' ||
[Link](i)=='A'
||[Link](i)=='E' ||[Link](i)=='I' ||[Link](i)=='O'
||[Link](i)=='U' )
{
vowel++;
}
else {
cons++;
}
}
}
float v=(vowel/len)*100;
float c=(cons/len)*100;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter String: ");
String str=[Link]();
[Link](percentageOfVowelsAndConsonent(str));
[Link]();
}
OUTPUT:
Enter String:
Consonents
Vowels : 30.00 %
Consonent: 70.00 %
CODE:
import [Link];
for(int i=0;i<len;i++) {
if([Link](i)!=' ') {
if(([Link](i)>= 'A' && [Link](i)<='Z')
) {
cap++;
}
else if(([Link](i)>= 'a' &&
[Link](i)<='z')) {
small++;
}
else if([Link](i)>='0' &&
[Link](i)<='9'){
number++;
}
else {
special++;
}
}
}
float c=(cap/len)*100;
float sm=(small/len)*100;
float d=(number/len)*100;
float s=(special/len)*100;
[Link](percentageOfSmallCapDigitSpecial(str));
[Link]();
}
}
OUTPUT:
Enter String:
Nacre@123%
Capital : 10.00%
Small : 40.00%
Digit : 30.00%
Special : 20.00%
CODE:
import [Link].*;
public class SortStr3 {
String str="";
char[] arr = new char[[Link]()];
int p=0;
for(int i=0;i<[Link]();i++){
char ch=[Link](i);
if(ch!=' ') {
arr[p++]=[Link](i);
}
}
for(int i=0;i<[Link]();i++) {
for(int j=i+1;j<[Link]();j++){
if((arr[i]>arr[j])&& i!=j) {
char c=arr[i];
arr[i]=arr[j];
arr[j]=c;
}
}
str += arr[i];
}
return str;
}
OUTPUT:
Enter String to sort:
avinash
aahinsv
for(int i=0;i<[Link]();i++) {
arr[i]=[Link](i);
}
for(int i=0;i<[Link]();i++) {
arr2[i]=[Link](i);
}
if([Link]()!=[Link]()) {
return "They have different size can't be Anagram";
}
else {
for(int i=0;i<[Link]();i++) {
for(int j=0;j<[Link]();j++) {
if(i!=j && arr[i]>arr[j]) {
char ch = arr[i];
arr[i]=arr[j];
arr[j]=ch;
}
if(i!=j && arr2[i]>arr2[j]) {
char ch = arr2[i];
arr2[i]=arr2[j];
arr2[j]=ch;
}
}
}
}
for(int i=0;i<[Link]();i++) {
if(arr[i]!=arr2[i]) {
return "Not Anagram";
}
}
return "They are Anagram.";
}
[Link](checkAnagram(str,str2));
[Link]();
}
OUTPUT:
Enter First String:
reaction
Enter Second String:
creation
They are Anagram.
OUTPUT:
Enter String:
Today is Monday
Enter char to search:
a
given character a occurrence is 2 times
22. Write a program to replace given character to
other given Character in the string?
CODE:
import [Link];
[Link](replaceCharacter(str,ch,re));
[Link]();
}
}
OUTPUT:
Enter String:
This is giil
Enter char to replace:
i
Enter string to replace with:
#
Th#s #s g##l
CODE:
import [Link];
if([Link](i)!=[Link](j)) {
[Link](checkPalindrome(str));
[Link]();
}
OUTPUT:
Enter string to check:
madam
It's Palindrome!
CODE:
import [Link];
public class ReverseSentence {
for(int j=k;j<[Link]();j++) {
if([Link](j)!=' ') {
temp += [Link](j);
k++;
}
else {
k++;
break;
}
}
arr[i] = temp;
}
for(int i=[Link]-1;i>=0;i--) {
result += arr[i]+" ";
}
return result;
}
[Link](reverseSentenseWithoutReverseWord(str));
[Link]();
}
OUTPUT:
Enter String to reverse:
Java is best programming language
language programming best is Java
CODE:
import [Link];
public class ReverseWords {
for(int i=0;i<[Link]();i++) {
if([Link](i)==' ') {
countSpace++;
}
}
for(int i=0;i<countSpace+1;i++) {
String temp="";
int len=0;
for(int j=k;j<[Link]();j++) {
if([Link](j)!=' ') {
k++;
len++;
}
else {
len++;
k++;
break;
}
}
if(i==countSpace) {
k++;
len++;
}
for(int j=k-2;j>=k-len;j--) {
temp += [Link](j);
}
arr[i] = temp;
}
for(int i=0;i<[Link];i++) {
result += arr[i]+" ";
}
return result;
}
[Link]();
}
OUTPUT:
Enter String to reverse:
Today is Monday
yadoT si yadnoM
CODE:
import [Link].*;
String first=[Link]();
String second=first;
[Link](second);
[Link]();
}
OUTPUT:
Enter String:
nacre
nacre
27. Write a program to concat two Strings?
Ex: str1=” nacre” str2=” software”;
Output: nacre software
CODE:
import [Link].*;
public class Concat {
[Link]();
}
}
OUTPUT:
Enter first string:
Avinash
Enter second string:
Kumar
Avinash Kumar
28. Write a Program to print short name of given
string
Ex: Sanjeeva Reddy Nagar Output: SR Nagar
CODE:
import [Link];
for(int i=0;i<[Link]();i++) {
if([Link](i)==' ') {
countSpace++;
}
}
if(countSpace==0) {
ans=str;
}
else {
int j=0,k=0,i;
for(int h=0;h<countSpace;h++) {
ans=ans+[Link](j);
for(i=k;i<[Link]();i++) {
if([Link](i)!=' ') {
j++;
k++;
}
else {
break;
}
}
j++;
ans += " ";
k++;
}
for(int n=k;n<[Link]();n++) {
ans += [Link](n);
}
}
return ans;
}
[Link](printShortName(str));
[Link]();
OUTPUT:
Enter name to short:
Sanjeeva Reddy Nagar
S R Nagar
29. Swap first and last charecter of a given String
input:- NacrE Output:-EacrN
CODE:
import [Link].*;
public class SwapFirstToLast extends SortArray {
String ans="";
for(int i=0;i<[Link]();i++) {
if(i==0) {
ans += [Link]([Link]()-1);
}
else if(i==[Link]()-1) {
ans += [Link](0);
}
else {
ans += [Link](i);
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link](swapFirstLast(str));
[Link]();
}
}
OUTPUT:
Enter String:
NacrE
EacrN
CODE:
import [Link];
for(int i=0;i<[Link]();i++) {
int count=0;
if([Link](i)!=' ') {
for(int j=i+1;j<[Link]();j++) {
if([Link](i)==[Link](j) &&(i!=j)) {
count++;
}
}
if(count==0) {
result += [Link](i);
}
}
}
return result;
}
OUTPUT:
Enter String:
Aabccdeeef
Aabcdef
CODE:
import [Link].*;
public class SecHighest {
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
if(arr[i]>arr[j]) {
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
return arr[n-2];
}
OUTPUT:
Enter size of array:
10
Enter Element:
1 8 7 6 9 4 3 2 5 6
8
CODE:
import [Link];
return [Link](arr);
}
if(i==j-1) {
[Link](str);
}
else {
for(int k=i;k<j;k++) {
str = stringPermutation(str,i,k);
calculatePermutation(str,i+1,j);
str = stringPermutation(str,i,k);
}
}
}
[Link]();
}
OUTPUT:
Enter String:
abc
abc
acb
bac
bca
cba
cab
import [Link];
int m=0,n;
for(int i=0;i<r;i++) {
n=0;
for(int j=c-1;j>=0;j--) {
array[m][n]=arr[j][i];
n++;
m++;
return array;
int r=[Link]();
int c= [Link]();
for(int j=0;j<c;j++) {
arr[i][j]=[Link]();
[Link]([Link](turnMatrixClockwi
se(arr,r,c)));
[Link](); }
OUTPUT:
Enter no of row:
3
Enter no of column:
3
Enter row element:
1 2 3
Enter row element:
4 5 6
Enter row element:
7 8 9
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
34. program of anticlockwise
CODE:
import [Link];
import [Link];
int m=0,n;
for(int i=r-1;i>=0;i--) {
n=0;
for(int j=0;j<c;j++) {
array[m][n]=arr[j][i];
n++;
m++;
}
return array;
int r=[Link]();
int c= [Link]();
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
arr[i][j]=[Link]();
[Link]([Link](turnMatr
ixAnticlockwise(arr,r,c)));
[Link]();
}
OUTPUT:
Enter no of row:
3
Enter no of column:
3
Enter row element:
1 2 3
Enter row element:
4 5 6
Enter row element:
7 8 9
CODE:
import [Link];
public class SortArray {
for(int i=1;i<n;i=i+2) {
temp=arr[n-1];
for(int j=n-1;j>i;j--) {
arr[j]=arr[j-1];
}
arr[i]=temp;
}
return arr;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]();
}
OUTPUT:
Enter Array Size:
10
Enter Array Element:
1 2 3 4 5 6 7 8 9 10
1 10 2 9 3 8 4 7 5 6