Following are the rules which must be followed by the program:
- Every word in the
stringmust be divided uptonletters each and reverse them from the start - Only those words must be reversed which are having length equal or greater than
n
For example, if string = "law of attraction" and input n = 3.
Here, the length of of is less than n, thus must not be reversed, but law and attraction are having length greater than or equal to 3 thus must be reversed as
law as wal and
attraction as ttacaroitn
Final output: wal of ttacaroitn
In my given code, i can only able to reverse the string, but not words based on the input digit. Not getting any idea.
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println("Enter String: "+str);
Scanner sc1 = new Scanner(System.in);
int Digit = sc.nextInt();
int count = str.split("\\s+").length;
System.out.println("No.of words: "+count);
System.out.println("Given string: "+str);
if(Digit<=count) {
String s[] = str.split(" ");
String ans = "";
for (String part : str.split(" ")) {
System.out.print(new StringBuilder(part).reverse().toString());
System.out.print(" ");
}
}
else {
System.out.println("Your input Digit is Greater than count of words.");
}