Click here to Skip to main content
15,881,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to find frequency of characters in each line of a passage in C?
I've managed to count the frequency of all characters in a given passage but can't find the frequency of characters line by line.

What I have tried:

#include<stdio.h>
#include<string.h>
int main()
{
int i;
char str[1000];
int arr[26]={0};
gets(str);
for(i=0;i<strlen(str);i++)
{
 if(str[i]>='a' && str[i]<='z')
 {
 int temp=str[i]-'a';
 arr[temp]++;

 }
else if(str[i]>='A' && str[i]<='Z')
{

int temp=str[i]-'A';
arr[temp]++;

}
else
continue;

}
for(i=0;i<26;i++)
{

printf("%c=%d\n",i+'a',arr[i]);


}





}
Posted
Updated 6-Sep-22 21:08pm
v2

Quote:
How to find frequency of characters in each line of a passage in C?

All what you do to get the result for a single input, you will have to do for each input line.
You need to find how to input many times, with how to know that input is finished. With every input line , apply same procedure to get frequency on the line.
Nota: you may want to skip letters seen zero times, and you may want to have more than a single letter per result line.

Advice: learn to search C++ reference gets - C++ Reference[^]

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <stdio.h>
#include <string.h>

int main()
{
	int i;
	char str[1000];
	int arr[26] = { 0 };
	gets(str);
	for (i = 0; i < strlen(str); i++)
	{
		if (str[i] >= 'a' && str[i] <= 'z')
		{
			int temp = str[i] - 'a';
			arr[temp]++;
		}
		else if (str[i] >= 'A' && str[i] <= 'Z')
		{

			int temp = str[i] - 'A';
			arr[temp]++;
		}
		else
			continue;
	}
	for (i = 0; i < 26; i++)
	{

		printf("%c=%d\n", i + 'a', arr[i]);
	}
}

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
Comments
merano99 7-Sep-22 3:49am    
If one assumes that there is a sorted code table and no umlauts or the like are to be counted, this works.
But there are also functions in C to check letters. To use gets() and str safely with strlen(), I would initialize them with zeros like arr.
Also, it would be highly recommended to use gets_s() or fgets() instead of gets() to make sure there is no overflow. (The current version of Visual Studio doesn't seem to know gets() at all ?). Because of int main(), a return would also be formally expected.
My 5.
Patrice T 7-Sep-22 6:29am    
Thank you
CPallini 7-Sep-22 8:50am    
5.
Patrice T 7-Sep-22 8:55am    
Thank you
The isalpha, toupper, functions are handy, for such a task:
C
#include <stdio.h>
#include <ctype.h>

enum
{
  LETTERS = 'Z'-'A'+1,
};

int main()
{
  int c;
  int freq[LETTERS] = {0};
  while ((c = getchar()) != EOF)
  {
    if ( c == '\n')
    {
      for (int n=0; n<LETTERS; ++n)
      {
        printf("%c %4d\n", ('A'+n), freq[n]);
        freq[n] = 0;
      }
    }
    else
    {
      if ( isalpha(c) )
      {
        ++freq[toupper(c)-'A'];
      }
    }
  }
  return 0;
}
 
Share this answer
 
Comments
merano99 7-Sep-22 3:54am    
My 5
Patrice T 7-Sep-22 7:44am    
What if input is lowercase ?
+5
CPallini 7-Sep-22 8:48am    
++freq[toupper(c)-'A'];
takes care of it.
BTW Thank you!
Patrice T 7-Sep-22 8:55am    
missed it :)
The first thing I would suggest you do is to create a function to do most of the work. The function should accept a string and build the frequency histogram (i.e. count) of all the characters within it. It can then either return the result as an array, or print the details. The main body of the program then just needs to read each line, print the line number (and maybe the line), call the function and see the results.
 
Share this answer
 
In addition to what Richard stated, I recommend that you accept your input by reading a text file. You can open a file by calling fopen - C++ Reference[^] and you can read lines of text by calling fgets - C++ Reference[^]. That second page has sample code to show you how to use it.
 
Share this answer
 
Writing your own get_line(str) might do the trick
 
Share this answer
 
v2
Comments
Richard MacCutchan 6-Sep-22 11:50am    
Possibly, but neither C or C++ has such a function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900