Frequently Asked Java Program 27: How To Count Words in a String

preview_player
Показать описание
Topic : How To Count Words in a String

#########################
Udemy Courses:
#########################

Manual Testing+Agile with Jira Tool
************************************

Selenium with Java+Cucumber
********************************

Selenium with Python & PyTest
********************************

Selenium with python using Robot framework
****************************************

API Testing(Postman, RestAssured & SoapUI)
*****************************************

Web & API Automation using Cypress with Javascript
********************************************

Playwright with Javascript
**************************

Jmeter-Performance Testing
************************

SDET Essencials(Full Stack QA)
*************************

Appium-Mobile Automation Testing
************************************

Java Collections
*****************

Python Programming
*********************

Cucumber BDD Framework
***************************

Protractor with Javascript
***************************

####################################
Youtube Playlists:
####################################

Manual Testing & Agile
***********************

SQL
*************************

linux & Shell Scripting
**********************

Java
**********************

Selenium With Java+Cucumber
********************************

Python
********************************

Selenium With Python,Pytest&Behave
***************************************

Selenium With Python Using Robert Framework
(Web&API Testing)
*************************************************

API Testing (Postman,SoapUi,&Rest Assured)
**********************************************

Mobile App Testing Appium
****************************

Performance Testing Jmeter
*******************************

Maven,Jenkins,Git,Github,CI/CD
*******************************

SQL,DB Testing&ETL,Bigdata
*******************************

JavaScript Based Automation Tools
********************************

Selector Hub Tools
********************

GraphQL
******************

Cypress API Testing
********************

Cypress Web Testing
**********************

Playwright with Javascipt
**************************

#PythonWordCount
#StringWordCount
#WordCountProgram
#PythonStringOps
#CodingInPython
#StringManipulation
#PythonProgramming
#TextProcessing
#StringFunctions
#PythonBeginners
#CodingTips
#PythonCoding
#PythonTricks
#ProgrammingLogic
#PythonSnippets
#TechHelp
#CodeExamples
#CodingCommunity
#PythonDev
#PythonSolutions
#ProgrammingInPython
#LearnToCode
#CodeNewbie
#CodeItOut
#CodingMadeEasy
#StringParsing
#AlgorithmPractice
#CodeLearning
#PythonAlgorithms
#StringOperations
#PythonStrings
#CodeMastery
#ProgrammingHelp
#CodeAcademy
#PythonTips
#WordCountChallenge
#StringHandling
#CodingJourney
#CodeSnippets
#PythonProblems
#ProblemSolving
#CodingSolutions
#PythonExplained
#StringAnalysis
#CodeDebugging
#PythonCommunity
#ProgrammingQueries
#PythonParsing
#CodeGuidance
#LearnPython
Рекомендации по теме
Комментарии
Автор

Easiest way to solve :
class Test{
public static void main(String [] args) {
String str = "hello walcome to java programming";
String []tokens=str.split(" ");

} }

jashuvadevisetti
Автор

There is one more easy approach.
String str = “Welcome to the Java tutorials”;
String strArray[]=str.split(“ “);
System.out.println(“The number of words in a string is “ + strArray.length);

rajendramahapatra
Автор

public static void main(String[] args) {
int count=1;
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
for(int i=0;i<s.length();i++)
{
if((s.charAt(i)==' '))
{
count++;
}
}
System.out.println("Number of words in string = "+count);



}



There is one less condition and giving same output

harshitranjanrai
Автор

One more easy approach. String sentence = "Java easy peasy when we practise and explore";
String[] words = sentence.split("\\W+");

chinna
Автор

can we use this ?


String s1 ="This is a java program to find the words in the string";
String [] s2 =s1.split(" ");
System.out.println("Number of words are " +s2.length);

charanbudama
Автор

You can actually use the Scanner class for counting letters:





private static int cntWords(String phrase) {

Scanner in = new Scanner(phrase);
int cnt = 0;
while (in.hasNext()) {
cnt++;
in.next();
}
in.close();

return cnt;
}

MartinoxxHD
Автор

int count = s.replaceAll(" ", "").length();
System.out.println(count);
i think this looks pretty easy
love you Sir for ur dedication to videos

Developer
Автор

String S = "Hidndustan India Ganga";

String [] words = S.split(" ");


pankajsahu
Автор

Hello Pavan sir,
This program fails if we haven't entered any string as input, the count is showing 1, bcoz by default count is 1. Another thing if the user entered multiple spaces as input then what logic needs to add to find the total count?

nileshpardeshi
Автор

import java.util.Scanner;
public class CountwordsinaString {
public static void main (String args []) {
Scanner in = new Scanner(System.in);
System.out.println(" Enter a String");
String str=in.nextLine();


int count=1;
for(int i=0; i<str.length(); i++) {
if(str.charAt(i)==' ') {
count=count+1;
}
}
System.out.println(" numbers of word present in the string is "+count);
}
}

raghusundaram
Автор

It's very easy solution...
Thanks brother...

saurabhpadwal
Автор

Awesome solution 👏🏾👌🏾👍🏽!!! Thanks for this!!!!

EdClarkHYCT
Автор

Thank you sir ! Really Helped allot ❤❤

shailendraverma
Автор

For loop should be i<=s.length()-1. Anyway here it is doesn't matter

tapaskhandai
Автор

i<s.length-1 or it should be i<s.length

saqlainquazi
Автор

some days earleir, Mr. Pavan had shared a link for a new youtuber for learning java and other stuffs.Do anyone know what is the name of the channel?

dilrubaahmed
Автор

there are one test case fail in your code ex." atue auah iaajja " output require=3 but your code output is 4

hefshineatulashokugalmugal
Автор

I have one doubt, If we have space in starting of the sentence, we need to keep cunt as 0, but t wont work for count 1.Please suggest!

Shreenidhi
Автор

why the count variable is intialized with 1 not zero?

subhamanand
Автор

one confusion : in if you used compare "==" for first condition and for 2nd "=" only assignment could you please explain

nainagupta