Count occurrences of a char in plain text file
How about this:
fgrep -o f <file> | wc -l
Note: Besides much easier to remember/duplicate and customize, this is about three times (sorry, edit! botched the first test) faster than Vereb's answer.
Counting the occurrence of characters within a text file in Java
You are initializing variable count
in every iteration, which is wrong. Declare variable outside of while
and check it will work. From your code it will print only the last line count.
Please do following change to your code:
public class LetterCounter
{
public static void main( String[] args ) throws FileNotFoundException
{
Scanner input = new Scanner( new File( "src/para1.txt" ) );
char someChar = 'a';
int count = 0;
while ( input.hasNextLine() )
{
String answer = input.nextLine();
answer = answer.toLowerCase();
for ( int i = 0; i < answer.length(); i++ )
{
if ( answer.charAt( i ) == someChar )
{
count++;
}
}
System.out.println( answer );
}
System.out.println( "a - " + count );
input.close();
}
}
Counting All Character Occurrences in a Text File in C
This is because of fgets() . It considers until it encounters a newline character. you are reading line by line. Either you can use static int n = 0; or do
int k = 0;
ficheiro1 = fopen("encrypted_file.txt", "r");
while(fgets(texto_str, 3000, ficheiro1) != NULL)
{
k = k +strcountc(texto_str,'a' );
}
printf("no of char\t %d", k);
Count occurrences of character in file -C
There are multiple problems in your code:
ch
must be defined asint
to handleEOF
reliably.while (!feof(fp))
is incorrect. Learn Why is “while ( !feof (file) )” always wrong? You should instead write:while ((ch - getc(fp)) != EOF)
strcmp(&ch, ":")
is incorrect because&ch
is not a C string. You should usech == ':'
instead.you forget to close the file, causing a resource leak.
Here is a modified version:
int NumColon(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
return -1;
}
int count = 0;
int c;
while ((c = getc(fp)) != EOF) {
count += (c == ':');
}
fclose(fp);
return count;
}
Counting Character usage in text file? C
Rewind the pointer to the beginning of the file at the end of your for loop?
This has been posted before: Resetting pointer to the start of file
P.S. - maybe use an array for your output values : int charactercount[pow(2,sizeof(char))] so that you don't have to parse the file repeatedly?
edit: was missing pow()
Count number of character occurrences from input text file
In order to convert each String
into its representing characters, you need an additional flatMap
:
val characters = lines.flatMap(_.split(" ")).flatMap(_.toCharArray)
scala> val lines = Array("hello world", "yay more lines")
lines: Array[String] = Array(hello world, yay more lines)
scala> lines.flatMap(_.split(" ")).flatMap(_.toCharArray)
res3: Array[Char] = Array(h, e, l, l, o, w, o, r, l, d, y, a, y, m, o, r, e, l, i, n, e, s)
Although this is a Scala console, it will work the same on an RDD
.
Related Topics
Error: Ld.So: Object Ld_Preload Cannot Be Preloaded: Ignored
Arch Linux - Apt-Get Update Equivalent Command
Gzip Multiple Files Individually and Keep the Original Files
Pipe Output to Use as the Search Specification for Grep on Linux
How to Extract Characters Between the Delimiters Using Sed
Postgresql Database Default Location on Linux
How to Read a .Properties File Which Contains Keys That Have a Period Character Using Shell Script
Nginx Not Listening to Port 80
Update-Alternatives: Warning: /Etc/Alternatives/Java Is Dangling
Script to Change Password on Linux Servers Over Ssh
How to Reinstall the Latest Cmake Version
Why Is My Bash Script Adding <Feff> to the Beginning of Files
How Were the Weightings in the Linux Load Computation Chosen
Sighup for Reloading Configuration
How to Get a List of All Valid Ip Addresses in a Local Network