Learn-dsa..in 30 days!



























CC-14 : Count lines, word and characters in given text.

Description:

Given input string str that contains text in multiple lines, write a program that counts and returns the number of lines, words and characters in the input text. Assume all word are separated by space or line break.

Test cases and expected outputs:

Input Parameters Expected outputs
The input string is:
The String class stores character strings. All string literals in Java,
such as "abc", are implemented as Strings.Strings are immutable; their
values cannot be changed once they are created. StringBuffers support changeable strings.
As String objects are unchangeable they can be shared.
Count of Lines: 4
Count of Words: 42
Count of Characters: 246
The original string is:
Mount Everest is the highest mountain
Count of Lines: 1
Count of Words: 6
Count of Characters: 32

Pseudocode:

The java method should accept following input parameters: str (String).
Initialize char array chars=str.toCharArray().
Initialize int variable lCnt-0, we will use this variable to count number of lines in the input text.
Initialize int variable wCnt-0, we will use this variable to count number of words in the input text.
Initialize int variable cCnt-0, we will use this variable to count number of characters in the input text.
Iterate through chars using for loop using idx as loop variable, starting from index 0 and incrementing idx till we reach chars.length-1:
If chars[idx] is not space or ‘\n’ line break character then increment cCnt by 1.
If chars[idx] is space, we have found the end of a word, increment wCnt by 1.
If chars[idx] is ‘\n’ we have found a line break:
Increment lCnt by 1.
If the character preceding ‘\n’ is not a space, we have found a word at then end of line, so increment wCnt by 1.
After above loop is completed we have the counts of lines, words and characters. Return lCnt, wCnt and cCnt from the program as part of an integer array.

The above algorithm can also be completed using Java String APIs:

Initialize int variable lCnt-0, we will use this variable to count number of lines in the input text.
Initialize int variable wCnt-0, we will use this variable to count number of words in the input text.
Initialize int variable cCnt-0, we will use this variable to count number of characters in the input text.
Use str.lines() methods to covert str to a Stream of lines names strm.
Use Stream.toArray() method to covers strm to an array called lines.
Iterate through lines using for loop using lidx as loop variable, starting from index 0 and incrementing lidx till we reach lines.length-1:
Create a String array named words, initialize words=lines[lIdx].split(“ ”). This will tokenize the current string using space as a separator and return an array of tokenised words.
Iterate through words using for loop using widx as loop variable, starting from index 0 and incrementing widx till we reach words.length-1:
Set cCnt=words[wIdx]
After above inner loop completes, set increment wCnt by words.length; this will add count of words in current line to wCnt
After outer loop completes set lCnt to lines.length.
We have count of lines, words, characters in lCnt, wCnt, cCnt. Return lCnt, wCnt and cCnt from the program as part of an integer array.

Code:

public int[] stringCountLinesWordsChars(String str) {
	char[] chars=str.toCharArray();
	int lCnt=0; int wCnt=0; int cCnt=0;
	for (int idx=0; idx<chars.length; idx++) {
		if ((chars[idx] !=' ')&& (chars[idx]!= '\n')) {
			cCnt++;
		}
		if (chars[idx] ==' ') {
			wCnt++;
		}
		if ((chars[idx] =='\n') || (idx == chars.length-1)) {
			lCnt++;
			if ((idx-1 >= 0)&& (Character.isWhitespace(chars[idx-1])==false)){
				wCnt++;
			}
		}
	}	
	int[] retVal = {lCnt, wCnt, cCnt};	
	return retVal;
}

public int[] stringCountLinesWordsCharsWithAPI(String str) {
	
	int lCnt=0; int wCnt=0; int cCnt=0;
	Stream<String> strm=str.lines();
	Object[] lines= strm.toArray();
	for (int lIdx=0; lIdx < lines.length; lIdx++) {
		String[] words=((String)lines[lIdx]).split(" ");
		for (int wIdx=0; wIdx < words.length; wIdx++) {
			cCnt+=words[wIdx].length();
		}
		wCnt+=words.length;
	}
	
	int[] retVal = {lines.length, wCnt, cCnt};	
	return retVal;
}

Click here to download and run code and test cases !