首页 > 编程语言 >Java: How To Count Words

Java: How To Count Words

时间:2022-11-27 23:56:09浏览次数:41  
标签:Count Java String originalStr System How reversedStr public out

public class Main {
  public static void main(String[] args) {
    String words = "One Two Three Four";
    int countWords = words.split("\\s").length;
    System.out.println(countWords);
  }
}

// Outputs
4

Reverse a String

public class Main {
  public static void main(String[] args) {
    String originalStr = "Hello";
    String reversedStr = "";
    System.out.println("Original string: " + originalStr);
        
    for (int i = 0; i < originalStr.length(); i++) {
      reversedStr = originalStr.charAt(i) + reversedStr;
    }
    
    System.out.println("Reversed string: "+ reversedStr);
  }
}

 

标签:Count,Java,String,originalStr,System,How,reversedStr,public,out
From: https://www.cnblogs.com/ShengLiu/p/16931061.html

相关文章

  • Java: Files
    JavaFileHandlingThe File classfromthe java.io package,allowsustoworkwithfiles.importjava.io.File;//ImporttheFileclassFilemyObj=new......
  • java9
    Java9模块系统Java9最大的变化之一是引入了模块系统(Jigsaw项目)。模块就是代码和数据的封装体。模块的代码被组织成多个包,每个包中包含Java类和接口;模块的数据则包括资......
  • SpringBoot(四):java从配置文件中取值的方式
    一、SpringBoot项目中取yaml配置文件中的值application.yamltest:url:localhost:8080name:rootpassword:123456val:a:1b:2c:3TestC......
  • Java: Threads
    Threadsallowsaprogramtooperatemoreefficientlybydoingmultiplethingsatthesametime.CreatingaThreadTherearetwowaystocreateathread.Itcan......
  • Java: Regular Expressions
    Pattern Class-Definesapattern(tobeusedinasearch)Matcher Class-UsedtosearchforthepatternPatternSyntaxException Class-Indicatessyntaxe......
  • Java中使用正则表达式
    1、使用 java.util.regex.Pattern类的 compole(表达式)方法把正则表达式变成一个对象。//表达式对象:1个数字和1个字母连续Patternpattern=P......
  • 拓端tecdat|R语言代写周氏检验(Chow test) 检验回归中结构不稳定性的虚拟变量的替代方
    全球化时代快速增长的经济体之一是埃塞俄比亚经济。在低收入国家中,它已成为在国内生产总值(GDP)中实现两位数增长率的少数几个国家之一。然而,关于两位数的增长率存在很多争论,......
  • Java入门代码练习
    一、第一个Java程序1、helloworldpublicclassHello{publicstaticvoidmain(String[]args){System.out.println("Helloworld!");}}2、变量i......
  • 单片机中简单的时延程序void Delay(__IO uint32_t nCount)
    这个延时函数没有返回值,函数类型是void型,,函数名称是Delay,同时函数的参数部分中的_IO是类型修饰符,指的是单片机的静态IO口;u32是数据类型,是指32位的无符号整形变量。,......
  • Java Excel导出动态自定义单元格样式
    根据表格内容定义单元格样式效果图:文章描述两种,一种创建生成时定义样式,另一种在excel在写入文件前修改样式关键代码一/***数据动态设置样式*......