write a program to arrange the characters of a string in alphabetical order in java
Share
write a program to arrange the characters of a string in alphabetical order in java
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Explanation:
import java.util.Scanner;
public class Position
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the k th position at which you want to check number:");
int k = s.nextInt();
System.out.println("Number:"+a[k-1]);
}
}
Output:
$ javac Position.java
$ java Position
Enter no. of elements you want in array:5
Enter all the elements:
2
5
3
8
6
Enter the k th position at which you want to check number:3
Number:3
Answer:
import java.util.*;
class alphabets
{
Scanner sc=new Scanner(System.in);
int i,l,j;
String str="",ns="";
char ch;
void input()
{
System.out.println("Enter the string");
str=sc.nextLine();
l=str.length();
}
void cal()
{
ns=str.toUpperCase();
for(i=65;i<=90;i++)
{
for(j=0;j<l;j++)
{
ch=ns.charAt(j);
if(ch==(char)i)
System.out.print(ch);
}
}
}
public static void main()
{
alphabets ob=new alphabets();
ob.input();
ob.cal();
}
}
Explanation:
INPUT:
Enter the string
COMPUTER
OUTPUT:
CEMOPRTU