计算一个字符串在一个字符串中出现的次数
Counting the number of occurences of a string inside a string
这是我对这种方法的尝试。
Count the number of co-occurrances of a non-empty sub-string sub within the string str E.g.
numOccurances("dogmonkeydog","dog") will return 2
numOccurances("dogmonkeydog","mon") will return 1
numOccurances("dogmonkeydog","cow") will return 0
public static int numOccurrences(String str, String sub) {
int result = 0;
int pos = str.indexOf(sub);
if (pos == -1){
return result;
}
if (sub.length() > str.length()){
return result;
}
if ((str.substring(0, sub.length())).equals(sub)){
result++;
String st = str.substring(pos);
return result + numOccurrences(st, sub); //Line 87
}
else{
String st = str.substring(sub.length());
return result + numOccurrences(st, sub);
}
}
我对结果 > 0 的所有测试都失败了
java.lang.WhosebugError
at java.lang.String.indexOf(String.java:1718)
at java.lang.String.indexOf(String.java:1698)
at eecs2030.lab6.RecursiveTasks.numOccurrences(RecursiveTasks.java:77)
at eecs2030.lab6.RecursiveTasks.numOccurrences(RecursiveTasks.java:87)
我不确定为什么我的代码从未达到其基本情况,任何见解将不胜感激!
这似乎是一项学校作业。所以,我不会直接给你答案。
在下面的片段中,
result++;
String st = str.substring(pos);
return result + numOccurrences(st, sub); //Line 87
你创建的地方st
有问题。如果 str
以 sub
中包含的值开头,则 str
将等于 st
。因此,对 numOccurrences
的调用将与您的原始调用相同,因此您的递归不会终止。
分析上面代码段中需要传递给str.substring
的内容。
希望对您有所帮助!
public static int findOcc(String str,String sub){
int count =0;
int occurence = 0;
char strArray [] = str.toCharArray();
char subArray [] = sub.toCharArray();
for(int i=0;i<strArray.length;i++){
if(strArray[i]==subArray[0]){
for(int j=0,k=i;j<subArray.length && k < strArray.length
;j++,k++){
if(strArray[k]==subArray[j]){
count++;
}
}
if(count == subArray.length){
occurence++;
}
count =0;
}
}
return occurence;
}
不需要你方法中的第三个 if 条件。
if ((str.substring(0, sub.length())).equals(sub))
第三种情况可以简单定义为
if(pos>=0)
{
result++;
String newstr = str.substring(pos + sub.length());
return numOccurrences(newstr,sub);
}
因为如果找到子串,pos 变量将通过子串的起始索引进行初始化,您可以在此处递增结果。
然后对字符串的其余部分递归调用 numOccurences() 方法。
并在方法外声明变量结果。
import java.util.Scanner;
class SubString
{
String user,subUser;
Scanner sc=new Scanner(System.in);
int num;
static int result = 0;
int numOccurrences(String str, String sub) {
int pos = str.indexOf(sub);
if (pos == -1){
return result;
}
if (sub.length() > str.length()){
return result;
}
else if(pos >= 0)
{
result++;
String newstr = str.substring(pos + sub.length());
return numOccurrences(newstr,sub);
}
return result;
}
//constructor
SubString()
{
try{
System.out.println("Enter string :");
user=sc.nextLine();
System.out.println("Enter the substring: ");
subUser=sc.nextLine();
num = numOccurrences(user,subUser);
System.out.println(num);
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String...a)
{
new SubString();
}
}
`
1 : public static int numOccurrences(String str, String sub) {
2 : int result = 0;
3 : int pos = str.indexOf(sub);
4 : if (pos == -1){
5 : return result;
6 : }
7 : if (sub.length() > str.length()){
8 : return result;
9 : }
10: if ((str.substring(0, sub.length())).equals(sub)){
11: result++;
12: String st = str.substring(pos);
13: return result + numOccurrences(st, sub);
14: }
15: else{
16: String st = str.substring(sub.length());
17: return result + numOccurrences(st, sub);
18: }
19:}
第 10 行 - 您没有在实际字符串 str
.
中提取给定 sub
字符串参数的子字符串
第 12 行 - 您几乎已经放弃了下一次调用的当前子字符串,但您也包含了该字符串。
示例:monstrfri -> 子字符串 str 将导致 strfri 而不是 fri
如果你想改变你的逻辑,你可以通过一个简单的 while 循环来实现
pos 是子字符串的索引
counter is 0
while pos!=-1
increment the counter
trim the current substring found
extract the next position
and continue while
这是我对这种方法的尝试。
Count the number of co-occurrances of a non-empty sub-string sub within the string str E.g.
numOccurances("dogmonkeydog","dog") will return 2
numOccurances("dogmonkeydog","mon") will return 1
numOccurances("dogmonkeydog","cow") will return 0
public static int numOccurrences(String str, String sub) {
int result = 0;
int pos = str.indexOf(sub);
if (pos == -1){
return result;
}
if (sub.length() > str.length()){
return result;
}
if ((str.substring(0, sub.length())).equals(sub)){
result++;
String st = str.substring(pos);
return result + numOccurrences(st, sub); //Line 87
}
else{
String st = str.substring(sub.length());
return result + numOccurrences(st, sub);
}
}
我对结果 > 0 的所有测试都失败了
java.lang.WhosebugError
at java.lang.String.indexOf(String.java:1718)
at java.lang.String.indexOf(String.java:1698)
at eecs2030.lab6.RecursiveTasks.numOccurrences(RecursiveTasks.java:77)
at eecs2030.lab6.RecursiveTasks.numOccurrences(RecursiveTasks.java:87)
我不确定为什么我的代码从未达到其基本情况,任何见解将不胜感激!
这似乎是一项学校作业。所以,我不会直接给你答案。
在下面的片段中,
result++;
String st = str.substring(pos);
return result + numOccurrences(st, sub); //Line 87
你创建的地方st
有问题。如果 str
以 sub
中包含的值开头,则 str
将等于 st
。因此,对 numOccurrences
的调用将与您的原始调用相同,因此您的递归不会终止。
分析上面代码段中需要传递给str.substring
的内容。
希望对您有所帮助!
public static int findOcc(String str,String sub){
int count =0;
int occurence = 0;
char strArray [] = str.toCharArray();
char subArray [] = sub.toCharArray();
for(int i=0;i<strArray.length;i++){
if(strArray[i]==subArray[0]){
for(int j=0,k=i;j<subArray.length && k < strArray.length
;j++,k++){
if(strArray[k]==subArray[j]){
count++;
}
}
if(count == subArray.length){
occurence++;
}
count =0;
}
}
return occurence;
}
不需要你方法中的第三个 if 条件。
if ((str.substring(0, sub.length())).equals(sub))
第三种情况可以简单定义为
if(pos>=0)
{
result++;
String newstr = str.substring(pos + sub.length());
return numOccurrences(newstr,sub);
}
因为如果找到子串,pos 变量将通过子串的起始索引进行初始化,您可以在此处递增结果。
然后对字符串的其余部分递归调用 numOccurences() 方法。 并在方法外声明变量结果。
import java.util.Scanner;
class SubString
{
String user,subUser;
Scanner sc=new Scanner(System.in);
int num;
static int result = 0;
int numOccurrences(String str, String sub) {
int pos = str.indexOf(sub);
if (pos == -1){
return result;
}
if (sub.length() > str.length()){
return result;
}
else if(pos >= 0)
{
result++;
String newstr = str.substring(pos + sub.length());
return numOccurrences(newstr,sub);
}
return result;
}
//constructor
SubString()
{
try{
System.out.println("Enter string :");
user=sc.nextLine();
System.out.println("Enter the substring: ");
subUser=sc.nextLine();
num = numOccurrences(user,subUser);
System.out.println(num);
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String...a)
{
new SubString();
}
}
`
1 : public static int numOccurrences(String str, String sub) {
2 : int result = 0;
3 : int pos = str.indexOf(sub);
4 : if (pos == -1){
5 : return result;
6 : }
7 : if (sub.length() > str.length()){
8 : return result;
9 : }
10: if ((str.substring(0, sub.length())).equals(sub)){
11: result++;
12: String st = str.substring(pos);
13: return result + numOccurrences(st, sub);
14: }
15: else{
16: String st = str.substring(sub.length());
17: return result + numOccurrences(st, sub);
18: }
19:}
第 10 行 - 您没有在实际字符串 str
.
中提取给定 sub
字符串参数的子字符串
第 12 行 - 您几乎已经放弃了下一次调用的当前子字符串,但您也包含了该字符串。
示例:monstrfri -> 子字符串 str 将导致 strfri 而不是 fri
如果你想改变你的逻辑,你可以通过一个简单的 while 循环来实现 pos 是子字符串的索引
counter is 0
while pos!=-1
increment the counter
trim the current substring found
extract the next position
and continue while