在右侧我们给出了一个已经基本完成的程序,读入了一个字符串,调用了一个叫str_len的函数来计算这个字符串的长度,并输出。 聪明的你应该已经发现了,这个叫str_len的函数并没有完成

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41367523/article/details/84558458

在右侧我们给出了一个已经基本完成的程序,读入了一个字符串,调用了一个叫str_len的函数来计算这个字符串的长度,并输出。

聪明的你应该已经发现了,这个叫str_len的函数并没有完成,在不修改函数原型的情况下,请完成str_len函数,实现我们上述的功能吧。

样例输入 复制
abcdefg
样例输出 复制
7

import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner input = new Scanner(System.in);
		String str = input.nextLine();
		System.out.println(str_len(str));
	}
	static int str_len(String str)
	{	//请在这里完成代码
		    int count=0;
            for (int i = 0; i < str.length(); i++) {
                count++;
            }
            return count;
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_41367523/article/details/84558458
今日推荐