[英]Find character frequency in a string
Im trying to find character frequency in a string,i wrote the following code,but it does'nt show any output.All im trying is to fill the character array with respective counts. 我试图在字符串中查找字符频率,我编写了以下代码,但未显示任何输出。所有我试图用相应的计数填充字符数组。
When i tried to debug,it some how gives output,but prints some garbage value. 当我尝试调试时,它会给出一些输出,但是会打印一些垃圾值。
#include<stdio.h>
/* Program for printing character frequency in string */
charcount(char *,int *);
int main()
{
int n,i=0;
printf("Enter n :");
scanf("%d",&n);
char var[n];
int count[100]; // array for storing character frequency
printf("Enter string :");
fflush(stdin);
scanf("%s",var);
charcount(var,count); // calling frequeny function
for(i=0;i<strlen(count);i++)
{
printf("%d\n",count[i]);
}
getch();
return 0;
}
charcount(char *p,int *q)
{
for(;*p;p++)
{
q[*p]++;
}
}
You have few problems in your code: 您的代码中有几个问题:
count
array is not initialized. count
数组未初始化。
You are applying strlen() on an integer array. 您正在将strlen()应用于整数数组。
count array should be 256 ( count[256]
) to cover all possible ascii chars. count数组应为256(
count[256]
),以覆盖所有可能的ascii字符。 For example, if your input is abcd
you'll go out of bound of array as d
is 100. 例如,如果输入为
abcd
,则由于d
为100,将超出数组的范围。
You are printing the wrong count: 您正在打印错误的计数:
printf("%d\\n",count[i]);
should be printf("%d\\n",count[var[i]]);
应该是
printf("%d\\n",count[var[i]]);
Declare proper prototype for charcount(). 为charcount()声明适当的原型。
After fixing these: 修复这些问题后:
#include<stdio.h>
/* Program for printing character frequency in string */
void charcount(char *,int *);
int main()
{
int n,i=0;
printf("Enter n :");
scanf("%d",&n);
char var[n];
int count[256]={0}; // array for storing character frequency
printf("Enter string :");
scanf("%s",var);
charcount(var,count); // calling frequeny function
for(i=0;i<strlen(var);i++)
{
printf("%d\n",count[var[i]]);
}
return 0;
}
void charcount(char *p,int *q)
{
for(;*p;p++)
{
q[*p]++;
}
}
Make sure to compile in C99 or C11 (eg gcc -std=c99 file.c
) mode as VLAs are not supported in earlier standards of C. 确保以C99或C11(例如
gcc -std=c99 file.c
file.c)模式进行编译,因为C的早期标准不支持VLA。
You need to initialize your count
array. 您需要初始化您的
count
数组。 Otherwise it will have garbage values in it by default. 否则,默认情况下它将包含垃圾值。 You can initialize the whole array to
0
like so: 您可以像这样将整个数组初始化为
0
:
int count[100] = {0};
int count
is nothing but a hashmap int count
不过是一个哈希表
Your code will not work for this string "abcd" 您的代码不适用于此字符串“ abcd”
count['a'] = val // Works fine ASCII value of a is 97
count['b'] = val // Works fine ASCII value of a is 98
count['c'] = val // Works fine ASCII value of a is 99
count['d'] = val ; // Undefined Behaviour ASCII value of d is 100
The size should be equal to ASCII set length 大小应等于ASCII设置的长度
int count[128] = {};
Your count
array may not be large enough to hold all printable values (even assuming ASCII), and it should be 0 initialized. 您的
count
数组可能不够大,无法容纳所有可打印的值(即使假定为ASCII),并且应将其初始化为0。 Your for
loop should be checking against the length of var
, not count
, since you cannot sensibly treat the count integer array as a string. 您的
for
循环应检查var
的长度,而不是count
,因为您不能明智地将count整数数组视为字符串。
int count[1<<CHAR_BIT] = {};
/*...*/
for(i=0;i<strlen(var);i++)
{
printf("%d\n",count[var[i]]);
}
Well, it really depends on what you want to output. 好吧,这实际上取决于您要输出的内容。 If you intend to output all of
count
, then: 如果您打算输出所有
count
,那么:
for(i=0;i<sizeof(count)/sizeof(count[0]);i++)
{
printf("%d\n",count[i]);
}
JAVA program to print "*" as much times as the occurrence of a character in String.
JAVA程序打印“ *”的次数与String中字符出现的次数相同。 Or char_in_String : frequency of char_in_String Instead of * you can print frequency count
或char_in_String:char_in_String的频率代替*您可以打印频率计数
public class CharFreq
{
public static void main(String[] args)
{
String s = "be yourself "; System.out.println(s);
int r=0;
char[] str = s.toCharArray();
for (int i = 0; i < str.length; i++)
{
int cnt = 0;
if (str[i] != ' ')
{
for (int j = 0; j < str.length; j++)
{
if (str[i] == str[j])
{
cnt++; r=j;
}
}
if(i==r)
{
System.out.print(str[i] + ":");
for (int k = 1; k <=cnt; k++)
System.out.print("*");
System.out.println();
}
}
}
}
}
Output: be yourself b:* y:* o:* u:* r:* s:* e:** l:* f:*
输出:做你自己b:* y:* o:* u:* r:* s:* e:** l:* f:*
Your count[100]
is not large enough. 您的
count[100]
不够大。 Assume you only enter "A - Z" or "a - z" it's still not large enough because 'z' is 122, then your charcount will increase count[122]. 假设您只输入“ A-Z”或“ a-z”,但由于“ z”为122,它仍然不够大,因此您的字符数将增加计数[122]。
You should consider change int count[100]
to int count[128] = { 0 }
您应该考虑将
int count[100]
更改为int count[128] = { 0 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.