英文:
Count Words Using indexOf
问题
public int howManyWords(String s) {
String myString = "I have a dream";
int count = 1;
int length = 0;
while (count >= 0) {
count = myString.substring(length, myString.indexOf(" "));
count++;
length = myString.indexOf(" ");
}
return count;
}
应返回 4。
英文:
I can't use arrays, only simple Java (if
, for
, while
, substring
, length
, indexOf
)
public int howManyWords(String s){
myString = "I have a dream";
int count = 1;
int length = 0;
while(count>=0){
count = myString.substring(String.valueOf(length),myString.indexOf(" "));
count++;
length = myString.indexOf(" ");
}
return count;
}
Should return 4
答案1
得分: 1
首先,你制造了一个无限循环,因为count
是1
,而你只是在增加它。
其次,你甚至没有尝试在某个集成开发环境(IDE)中编写这段代码,因为这会引发语法错误,当你执行count = myString.substring()
时,你将string
赋值给了int
。
所以,不要在循环中使用count
,你可以使用myString.indexOf
。
如果你不关心myString
会发生什么,类似下面这样的代码可能会起作用:
int count = 0;
while(myString.indexOf(" ") >= 0) {
count++;
myString = myString.substring(myString.indexOf(" ") + 1);
}
return count;
英文:
First of all, you made infinite loop, because count
is 1
, and you just increase it.
Second, you haven't even try to write this code in some IDE, because it would throw you a syntax error, because you are assigning string
to int
, when you do count = myString.substring()
So, instead of using count
in loop, you can use myString.indexOf
something like this could work if you don't care what is going to happen with myString
int count = 0;
while(myString.indexOf(" ") >= 0) {
count++;
myString = myString.substring(myString.indexOf(" ") + 1)
}
return count;
</details>
# 答案2
**得分**: 0
```java
public int countWords(String s){
String myString = "I have a dream";
int count = 0;
int length = myString.length();
while(length > 0){
if((myString.indexOf(" ") != -1) && (myString.indexOf(" ") + 1) < length){
myString = myString.subString(myString.indexOf(" ") + 1);
count++;
length = myString.length();
}
else {
length = 0;
break;
}
}
return count;
}
PS: Conventionally, your method names should denote actions, hence I suggested it to be countWords
instead of howManyWords
.
英文:
Edited : Added missing else case.
Try the following code :
Remove the counted words from your string using the substring and indexOf, and increment the count in each iteration.
public int countWords(String s){
String myString = "I have a dream";
int count = 0;
int length = myString.length();
while(length>0){
if((myString.indexOf(" ")!=-1) && (myString.indexOf(" ")+1)<length){
myString = myString.subString(myString.indexOf(" ")+1);
count++;
length = myString.length();
}
else {
length = 0;
break;
}
}
return count;
}
PS: Conventionally, your method names should denote actions, hence I suggested it to be countWords instead of howManyWords.
答案3
得分: 0
假设你正在测试的字符串不包含前导或尾随空格,因为这会影响解决方案。你问题中的示例字符串不包含前导或尾随空格。
在循环中简单地调用方法 indexOf(String, int)
,并且在每次迭代中,将 int
参数设置为比前一次迭代中获得的值大一。一旦方法 indexOf()
返回的值为 -1,表示完成。但不要忘记在退出循环后添加最后一个单词。
String myString = "I have a dream";
int count = 0;
int index = 0;
while (index >= 0 && index < myString.length()) {
index = myString.indexOf(" ", index);
System.out.println("index = " + index);
if (index >= 0) {
index++;
count++;
}
}
if (index < 0) {
count++;
}
System.out.println("count = " + count);
英文:
Let's assume that the string you are testing does not contain leading or trailing spaces, because that affects the solution. The example string in your question does not contain leading or trailing spaces.
Simply call method indexOf(String, int)
in a loop and in each iteration you set the int
parameter to one more than what you got in the previous iteration. Once the value returned by method indexOf()
is -1 (minus one), you are done. But don't forget to add the last word after you exit the loop.
String myString = "I have a dream";
int count = 0;
int index = 0;
while (index >= 0 && index < myString.length()) {
index = myString.indexOf(" ", index);
System.out.println("index = " + index);
if (index >= 0) {
index++;
count++;
}
}
if (index < 0) {
count++;
}
System.out.println("count = " + count);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论