Question 13: Write a Java program to count the number of lines, words, and characters in a text file.
Program code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class CountWords {
public static void main(String[] args) throws IOException
{
int nl = 0, nw = 0, nc = 0;
String line;
FileReader fr = new FileReader("D:\\textfile.txt");
BufferedReader br = new BufferedReader(fr);
while((line = br.readLine()) != null)
{
nl++;
nc += line.length();
StringTokenizer st = new StringTokenizer(line);
nw += st.countTokens();
}
System.out.printf("Number of lines: \t %d", nl);
System.out.printf("\nNumber of words: \t %d", nw);
System.out.printf("\nNumber of characters: \t %d", nc);
}
}
Output:
Number of lines: 2
Number of words: 9
Number of characters: 37
Question 14: Write a Java program to count the occurrences of each character in string.
Program code:
public class CountCharacter {
static int countLetters(String str, char ch)
{
if (str == null || str.length() == 0) {
return 0;
}
// Convert all letters into lowercase.
String s = str.toLowerCase();
int count = 0;
for (int i = 0; i < s.length(); i++)
{
if (ch == s.charAt(i))
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
int count = countLetters("String Programs", 's');
System.out.println("No of characters: " +count);
}
}
Output:
No of characters : 2
We can also find the number of occurrences of a character in a string using Java 8 streams and lambdas, and regular expressions. Let’s write simple code for it.
Program code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountCharacter
{
// Using Java 8 streams and lambdas
static long countOccurrencesOf(String str, char character)
{
String lowercase = str.toLowerCase();
return lowercase.codePoints().filter(ch -> ch == character).count();
}
// Using regular expressions.
static long countCharsUsingReg(String str, char character)
{
String lowercase = str.toLowerCase();
Pattern pattern = Pattern.compile("[^" + character + "]*" + character + "");
Matcher matcher = pattern.matcher(lowercase);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
}
public static void main(String[] args)
{
long count = countOccurrencesOf("String Programs", 's');
System.out.println("No of characters: " +count);
long count2 = countCharsUsingReg("String programs", 's');
System.out.println("Number of characters: " +count2);
}
}
Output:
No of characters: 2
Number of characters: 2
Question 15: Write a Java program to check two strings are anagram or not.
Refer to this tutorial for the best practice: Anagram in Java | How to Check it.
Question 16: Write a Java program to swap two strings without using a third or temporary variable.
Go to this tutorial for best practice: Java program to swap two strings in easy way.
In this tutorial, you have familiarized with the most important top 14+ String programs in Java asking in interview and technical test. Hope that you will have understood and practiced all Java string programs. These questions will help you understand the level of string programs.
Thanks for reading!!!





