Skip to content

Commit 7b2224a

Browse files
Merge pull request TheAlgorithms#21 from ashish4321/master
Added program for counting words in a string.
2 parents bba6735 + df20459 commit 7b2224a

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

.DS_Store

2 KB
Binary file not shown.

countwords.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Scanner;
2+
class CountTheWords
3+
{
4+
public static void main(String args[])
5+
{
6+
System.out.println("Enter the string");
7+
Scanner sc = new Scanner(System.in);
8+
String s=sc.nextLine();
9+
int count = 1;
10+
for (int i = 0; i < s.length()-1; i++)
11+
{
12+
if((s.charAt(i) == ' ') && (s.charAt(i+1) != ' '))
13+
{
14+
count++;
15+
}
16+
}
17+
System.out.println("Number of words in the string = "+count);
18+
}
19+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy