0% found this document useful (0 votes)
7 views5 pages

Java Program for Word and Character Count

Uploaded by

Pallavi More
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Java Program for Word and Character Count

Uploaded by

Pallavi More
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 2

Problem Statement:
Write a Java program that displays the no. of characters and

words in a text.

Algorithm:
1. Start the program.

2. Import java Scanner package.

3. Create a secondary class temp which includes two methods

void words(String str) and void characters(String str).

4. Define the methods using proper logic to count

words and characters and print them in the method itself.

5. In the main class, take the input of text from user

and display the menu. Read the choice.

6. Using switch case, call the methods accordingly in

each case.

7. Stop the program.


1
Code:

import [Link];

class temp {

void Words(String str)

int words=0;

int ws=0;

for(int i = 0; i < [Link](); i++)

if([Link](i)==' ')

ws++;

words=ws+1;

[Link]("The no. of words are:" +words);

void Charecters(String str)

int count=0;

for(int i = 0; i < [Link](); i++)

if([Link](i)!=' ')

count++;

}
}

[Link]("The no. of charecters are:"+count); }

class Assi2 {

public static void main(String[] args) {

int choice=0;

while(choice!=4)

Scanner sc = new Scanner([Link]);

[Link]("Enter the string:");

String str = [Link]();

[Link]("Enter your choice:");

[Link]("1. words"+"\n"+"2. characters"+"\n"+"[Link]");

choice = [Link]();

switch(choice)

case 1:

temp t1 = new temp();

[Link](str);

break;

case 2:

temp t2 = new temp();

[Link](str);

break;

case 3:

return;
}}

}}

Output:

Enter the string:

you are awesome

Enter your choice:

1. words

2. characters

3. Exit

The no. of words are: 3

Enter the string:

Welcome to Programming

Enter your choice:

1. words

2. characters

3. Exit

The no. of characters are:20

Enter your choice:

1. words

2. characters

3. Exit

You might also like