forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower.java
More file actions
72 lines (70 loc) · 1.85 KB
/
Copy pathpower.java
File metadata and controls
72 lines (70 loc) · 1.85 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.*;
public class power{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : ");
String inp = sc.nextLine();
boolean isOk = false;
int words=0;
for(int i=0;i<inp.length();i++){
char c = inp.charAt(i);
if(c>='A' && c<='Z' || c==' ' || c=='.' || c=='?' || c=='!'){
isOk=true;
if(c==' '){words+=1;}
}else{
System.out.println("INVALID OUTPUT");
isOk=false;
break;
}
}
if(isOk){// Getting each word of string into an array
String[] wrd_a = new String[words+1];
String wrd = "";
int words_n = words;
for(int i=0;i<inp.length();i++){
char c = inp.charAt(i);
if(c=='.' || c=='?' || c=='!'){
wrd_a[words_n]=wrd;
break;
}
if(c==' '){
wrd_a[words_n]=wrd;
words_n-=1;
wrd="";
}
else{wrd+=inp.charAt(i);}
}
int[] pow_a = new int[words+1];
for(int i=wrd_a.length-1;i>=0;i--){
String w = wrd_a[i];
int pow = 0;
for(int j=0;j<w.length();j++){
int c = w.charAt(j);
pow+=c;
}
pow_a[i]=pow;
}
for(int i=wrd_a.length-1;i>=0;i--){
System.out.println(wrd_a[i]+"="+pow_a[i]);
}
String incr = "";
int L=pow_a[0];
int n = pow_a.length;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(pow_a[j-1] < pow_a[j]){
int temp = pow_a[j-1];
pow_a[j-1] = pow_a[j];
pow_a[j] = temp;
String temps = wrd_a[j-1];
wrd_a[j-1] = wrd_a[j];
wrd_a[j] = temps;
}
}
}
for(int i=wrd_a.length-1;i>=0;i--){
System.out.print(wrd_a[i]+" ");
}System.out.println();
}
}
}