-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPotential_Sum_Of_Digits.java
More file actions
56 lines (56 loc) · 1.42 KB
/
Copy pathPotential_Sum_Of_Digits.java
File metadata and controls
56 lines (56 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
class Potential_Sum_Of_Digits
{
static int potentialOf(String w)
{
char c;
int p=0;
for(int i=0;i<w.length();i++)
{
c=w.charAt(i);
p=p+((int)c-64);
}
return p;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string :");
String s=sc.nextLine();
StringTokenizer st=new StringTokenizer(s);
int n=st.countTokens();
String words[]=new String[n];
int potential[]=new int[n];
int i=0;
String word;
while(st.hasMoreTokens())
{
word=st.nextToken();
words[i]=word;
potential[i]=potentialOf(word);
i++;
}
String temp;
int t;
for (int j = 0; j < n - 1; j++)
{
for (int k = j + 1; k < n; k++)
{
if (potential[j]>potential[k])
{
temp = words[j];
words[j] = words[k];
words[k] = temp;
t = potential[j];
potential[j] = potential[k];
potential[k] = t;
}
}
}
System.out.println("Changed string : ");
for(i=0;i<n;i++)
{
System.out.print(words[i]+" ");
}
}
}