Java program to count the number of words in a sentence | Java Strings Tutorials

preview_player
Показать описание
In today's episode, How to write a program to count number of words present in sentence using java, this is One of the most frequently asked programming interview questions in java, let`s explore how to write java code.

About the trainer: Mr. Manjunath Aradhya, a technocrat by profession, a teacher by choice and an educationist by passion. Under his able leadership, ABC for Technology Training which is a National brand enabling the creation of thousands of careers annually in the IT Sector. He has extensive experience working as a Business associate with Wipro Technologies. He has also served as a corporate trainer to many other leading software firms. He has been providing technical assistance to placement cell of various Engineering colleges. He has also authored numerous hot selling engineering and other textbooks which are published by Pearson Education, an internationally acclaimed publication house headquartered in London. Other renowned international publishers such as Cengage Learning headquartered in Boston, United States, have published books authored by him. C Programming and Data Structures book published by Cengage India Private Limited authored by a profound scholar Mr. Manjunath Aradhya is the prescribed book in Dr. Hari Singh Gour University which is the Central University and the oldest university in the state of Madhya Pradesh (MP), India

To learn more about Technology, subscribe to our YouTube channel:

#inbuiltmethodinjava, #inbuiltmethodsplit, #ArrayLength, #arraysinJavaScript, #Javainterviewquestions #ABC #ManjunathAradhya, #Technologybyaradhya, #JavabyAradhya

How to print duplicate characters present in a String using Maps in java

Introduction to Strings in Java

Computing length of String in C

Computing length of String in Java

Copying one String into another in Java

Reversing a String in Java

Palindrome Program in Java | Part 1

Palindrome Program in Java | Part 2

How to convert lowercase Strings to uppercase in Java | Part 1

How to Convert a String with spaces from lowercase to uppercase in Java | Part 2

Convert First Letter of Each Word From Lowercase to Uppercase in Java

How to count vowels consonants & special symbol in a string

How to verify if a String contains only integers

Pangram Program in Java

Loops in Java | for | while | do-while | for-each loop in java

Introduction to MAP Data Structure in Java

Accessing the Data in MAP Data Structure

Check if a String has ALL UNIQUE CHARACTERS using MAP Data Structure

How to print the First Non-Repeated character in a String using Maps in Java

How to print all Non-Repeated Characters in a String

How to find the maximum occurring character in a String using Maps in java

For more updates ABC Course, follow us on:
Contact us: 7676500600
Рекомендации по теме
Комментарии
Автор

Learn from Leaders, Learn from ABC.
Watch our latest technical videos in your language:

ABCforTechnologyTraining
Автор

class ABC {
public static void main( String args[])
{ String x="we are proud to ABCians "
String y[]= x.split(" ");
System.out.println(y.length);
}
}😍😍

magantisrinivas
Автор

String[] arr = { "ABC", "CDE", "TRE" };
Stream<String> stream = Arrays.stream(arr);
long i=stream.count();
System.out.println(i);

mowglishihtzutoy
Автор

Sir without using split method how to do this

sakshipawar
Автор

Not works well when first word is started with a space .

jaspreetsingh-nhkn
Автор

Bro this was one of the best java videos ever!

dennismc
Автор

Another way of writing the program without using split function


class CountWords
{
public static void main(String[] args)
{
String s="WE ARE PROUD INDIANS";
char y[]=s.toCharArray();
int size=y.length;
int i=0;
int count=1;
while(i!=size)
{
if(y[i]==' ')
{
count++;
}
i++;
}
System.out.println("The number of words in the sentence: "+count);
}
}

sudarshan
Автор

import java.util.Scanner;

//My code solves all the testcases:

public class countWords {

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("Enter a string:");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
//When we are giving this regex pattern, it considers all the extra spaces which can come in between words as well.If you give only (" ") any extra space will be considered as a word..so give the regex pattern.
String y[] =str.split("\\s+");

//If the string you give as input starts with space, it will be considered as a word as well.. so we must subtract minus 1 from length to counter it.
//So total 3 cases:
//1.NormalCase: Words have one space in between them will be executed by the else statement.
//2.Case2: Words starting with space will be executed by the if code.
//3.Case3: Words having more than just a single space between them..is already covered when we give the regex pattern.

if(str.startsWith(" ")) {

}
else {

}


}

}

keralacodingacademykca
welcome to shbcf.ru