You are given a string consisting of n lowercase Latin letters. You must find the count of number of larger alphabets for every character of the string (according to lexicographical order).
/* Counting no.of larger alphabets for every character of the string */
import java.io.*;
import java.util.*;
class Alpha_2B
{
public static void main(String args[])
{
String s1;
int i,j,c,n;
int a[] = new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter a string : ");
s1 = s.next();
n = s1.length();
char t;
char ch[] = s1.toCharArray(); // Converting String to charecter array
// Sorting charecters
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(ch[i] < ch[j])
{
t = ch[i];
ch[i]= ch[j];
ch[j] = t;
}
}
}
// Counting charecter
for(i=0;i<n;i++)
{
c=0;
for(j=0;j<n;j++)
{
if(ch[i] < ch[j])
{
c++;
}
}
a[i] = c;
}
// Printing sorted charecters
System.out.println("Sorted charecters and no.of large alphabets are :");
for(i=0;i<n;i++)
{
System.out.print(ch[i]);
}
System.out.print("\n");
// Printing number of large alphabets
for(i=0;i<n;i++)
{
System.out.print(a[i]);
}
}
}
Comments
Post a Comment