I have reversed the string and have a for loop to iterate through the reversed string. I am to count the characters and I know I have a logic flaw, but I cannot pinpoint why I am having this issue. The solution needs to return the count of the last word in the string. My first thought was the iterate through the string backward(idk why I decided to create a new string, I should have just iterated through it by decrementing my for loop from the end of the string) But the logic should be the same from that point for my second for loop. My logic is basically trying to count any characters that aren't whitespace in the last word, and then when the count variable has a value, as well as the next whitespace after the count has counted the characters of the last word. Any help would be appreciated!
class Solution {
public int lengthOfLastWord(String s) {
int count = 0;
int countWhite = 0;
char ch;
String reversed = "";
for(int i = 0; i < s.length(); i ) {
ch = s.charAt(i);
reversed = ch;
}
for(int i = 0; i < reversed.length(); i ) {
if(!Character.isWhitespace(reversed.charAt(i))) {
count ;
if(count > 1 && Character.isWhitespace(reversed.charAt(i)) == true) {
break;
}
}
}
return count;
}
}
CodePudding user response:
Maybe try this,
public int lengthOfLastWord(String s) {
String [] arr = s.trim().split(" ");
return arr[arr.length-1].length();
}
CodePudding user response:
Firstly, as you have mentioned, your reverse string formed is just a copy of your original string. To rectify that,
for (int i = s.length() - 1; i >= 0; i--) {
ch = s.charAt(i);
reversed = ch;
}
Secondly, the second if condition is inside your first if condition. That is why, it will never break ( because you are first checking if character is whitespace, if it is, then you are not going inside the if statement, thus your second condition of your inner if loop will never be satisfied).
public class HW5 {
public static void main(String[] args) {
String s = "My name is Mathew";
int count = lengthOfLastWord(s);
System.out.println(count);
}
public static int lengthOfLastWord(String s) {
int count = 0;
int countWhite = 0;
char ch;
String reversed = "";
System.out.println("original string is----" s);
for (int i = s.length() - 1; i >= 0; i--) {
ch = s.charAt(i);
reversed = ch;
}
System.out.println("reversed string is----" reversed);
for (int i = 0; i < reversed.length(); i ) {
if (!Character.isWhitespace(reversed.charAt(i)))
count ;
if (count > 1 && Character.isWhitespace(reversed.charAt(i)) == true) {
break;
}
}
return count;
}
}
=
and the output is :
original string is----My name is Mathew
reversed string is----wehtaM si eman yM
6
Another way to go about is : you use the inbuilt function split which returns an array of string and then return the count of last string in the array.
CodePudding user response:
Another option would be to use index of last space and calculate length from it:
public int lengthOfLastWord(String string) {
int whiteSpaceIndex = string.lastIndexOf(" ");
if (whiteSpaceIndex == -1) {
return string.length();
}
int lastIndex = string.length() - 1;
return lastIndex - whiteSpaceIndex;
}
String.lastIndexOf()
finds the start index of the last occurence of the specified string. -1
means the string was not found, in which case we have a single word and length of the entire string is what we need. Otherwise means we have index of the last space and we can calculate last word length using lastIndexInWord - lastSpaceIndex
.