Java 程序:计算字符串中字符总数

原文:https://www.studytonight.com/java-programs/java-program-to-count-the-total-number-of-characters-in-a-string

在本教程中,我们将学习如何计算字符串中的字符总数。但是在进一步深入之前,如果你不熟悉字符串的概念,那么一定要查看 Java 中Strings的文章。

输入:输入字符串:Hello World

输出:字符串中的字符总数为:10

上述问题可以通过三种方式解决:

方法 1:使用 for 循环

方法 2:使用 while 循环

方法 3:使用边做边循环

让我们分别看看这些方法。

程序 1:计算字符串中的字符总数

在这个程序中,我们将找到带有预定义值的字符串中的字符总数。这里,我们将使用 for 循环来计算字符串中的字符总数。

算法:

  1. 开始
  2. 声明一个字符串。
  3. 初始化字符串。
  4. 声明一个变量来计算给定字符串中的字符总数,并将其初始化为 0。
  5. 使用 for 循环进行计算。
  6. 使用 if 条件来避免计算空间。
  7. 每次角色遇到时增加计数。
  8. 打印给定字符串中的字符总数。
  9. 停下来。

下面是相同的代码。

/*Java Program to count the total number of characters in a string using a for loop*/
public class Main
{
  public static void main(String[] args) 
  {    
        String str = "Hello World";    
        int count = 0;    
        System.out.println("The entered string is: "+str);    
        //Count the characters in the string except space    
        for(int i = 0; i < str.length(); i++) 
        {    
            if(str.charAt(i) != ' ')    
                count++;    
        }                
        //Displays the total number of characters in the string    
        System.out.println("Total number of characters in the string: " + count);    
    }      
}

输入的字符串为:Hello World 字符串中的字符总数:10

程序 2:计算字符串中的字符总数

在这个程序中,我们将找到带有用户定义值的字符串中的字符总数。在这里,我们将要求用户输入值,然后使用 while 循环来计算字符串中的字符总数。

算法

  1. 开始吧。
  2. 声明一个变量来计算给定字符串中的字符总数,并将其初始化为 0。
  3. 声明一个字符串,并要求用户初始化它。
  4. 使用 while 循环计算给定字符串中的字符总数。
  5. 使用 if 条件避免计算空格。
  6. 如果字符遇到,递增计数变量。
  7. 打印给定字符串中的字符总数。
  8. 停下来。

下面是相同的 Java 语言代码。

/*Java Program to count the total number of characters in a string using a while loop*/
import java.util.*;
public class Main
{
  public static void main(String[] args) 
  {    
        //Take input from the user
        Scanner sc=new Scanner(System.in);    
        int count = 0,i=0;    
        System.out.print("Please Enter a String to Count Characters =  ");
        String str = sc.nextLine();
        //Use a while loop to calculate the total characters in the string
        while(i < str.length())
        {
            if(str.charAt(i) != ' ') 
            {
                count++;
            }
            i++;
        }        
        System.out.println("\nThe Total Number of Characters  =  " + count);        
    }      
}

请输入一个字符串来计算字符数=计算字符串长度 字符总数= 24

程序 3:计算字符串中的字符总数

在这个程序中,我们将找到带有用户定义值的字符串中的字符总数。在这里,我们将要求用户输入值,然后我们将使用 do-while 循环来计算字符串中的字符总数。

算法

  1. 开始吧。
  2. 声明一个字符串。
  3. 声明一个变量来计算字符串中的字符总数,并将其初始化为 0。
  4. 要求用户初始化字符串。
  5. 使用 do-while 循环计算字符串中的字符总数。
  6. do-while 循环检查条件 I < str . length();并执行循环,直到给定条件变为真。
  7. 使用 if 条件来避免计算空间。
  8. 打印给定字符串中的字符总数。
  9. 停下来。

下面是相同的 Java 语言代码。

/*Java Program to count the total number of characters in a string using a do-while loop*/
import java.util.*;
public class Main
{
  public static void main(String[] args) 
  {    
        //Take input from the user
        Scanner sc=new Scanner(System.in);    
        int count = 0,i=0;    
        System.out.println("Please Enter a String to Count Characters =  ");
        String str = sc.nextLine();
        //Use a while loop to calculate the total characters
        do 
        {
           if(str.charAt(i)!=' ')
           {
              // this condition is used to avoid counting space
              count++;
           }
            i++;
        }while(i<str.length());
        //Print the total number of characters in the given string
        System.out.print("The total number of character in a string:"+count);
    }      
}

请输入一个字符串来计数字符=这是一个边做边循环的例子 一个字符串中的字符总数:30