今兒看了個面試題,要求用遞歸實現求字符串長度,順手就把代碼寫出來了,希望能有人想出其他的方法
package offer.giveme;
//用遞歸實現求字符串長度
public class MyStr {
public int mystr(String s,int i){
if(i>=s.length())
return 0;
else
{
++i;
return 1+mystr(s,i);
}
}
public static void main(String[] args) {
String str="anbamdbmadk";
int length=new MyStr().mystr(str, 0);
System.out.println(length);
}
}