数一词的出现 [英] Count Occurence Of A Word

查看:72
本文介绍了数一词的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算单词文件"在数据部分中出现了多少次.我该怎么做?谢谢.

I''m trying to count how many times a word "file" occurs in the data part. How would I do this? Thank you.

void printRawData(unsigned char *data, int length, int more)
{
	int i, c=0;
	printf("     -------------One Data Begins-------------\n");
	for (i=0; i<length;>	{
		if ((data[i]>30 && data[i]<122) || 
			(((data[i]==10) || (data[i]==13) || (data[i]==123) || (data[i]==125))
            && (more>0)))
		{
			printf("%c", data[i]);
			c+=1;
                }
		else
		{
			printf("[%i]", data[i]);
			c+=3;
			if (data[i]>9) c++;
			if (data[i]>99) c++;
                }
		if (c>=47)
		{
			printf("\n");
			c=0;
                }
       }
}

推荐答案

看看 ^ ],使用它可以遍历字符串中的单词.

随着strtok的折旧和不安全,将strtok更改为strtok_s.

针对OP评论:
Have a look at strtok_s[^], with it you can iterate through the words of your string.

Changed strtok into strtok_s as strtok is depreciated and insecure.

In response to OP comment:
void printRawData( unsigned char* data, int length, int more )
{
	char* l_sToken = NULL;
	char l_sDelimiter[] = " ,\t\n";
	char* l_sNextToken = NULL;
	int l_nCount = 0;

	l_sToken = strtok_s( (char*)data, l_sDelimiter, &l_sNextToken );
	while( l_sToken != NULL )
	{
		if( strcmp( l_sToken, "Content" ) == 0 )
			l_nCount++;

		l_sToken = strtok_s( NULL, l_sDelimiter, &l_sNextToken );
	}
}


转换为字符串并使用现有方法可能更容易.如果使用MFC,则可以使用CStringCString::Find();如果不使用MFC,则可以使用std::stringstring::find().
It''s probably easier to convert to a string and use existing methods. If using MFC, you can use CString and CString::Find() or you can use a std::string and string::find() if you''re not using MFC.


值是哪里那算字吗? more的含义是什么? c应该为您做什么?
警告-此代码段仅适用于单词文件不重叠的情况:
Where is the value that counts the words? Whats the meaning of more? What should c do for you?
WARNING - this snippet only works because the word file doesnt overlap themselves:
int printRawData(const char *data, int length, int more)
{
  int      i,c;

  printf("     -------------One Data Begins-------------\n");
  for(c=i=0;i<length;)
  {
    if('f'!=data[i++]) continueif(length<=i) break;
    if('i'!=data[i++]) continueif(length<=i) break;
    if('l'!=data[i++]) continueif(length<=i) break;
    if('e'!=data[i++]) continue;
    ++c;
  }
  return c;
}

int _tmain(int argc, _TCHAR* argv[])
{
  const char*    text = "afile bfile filec file fixle files and so on file fil";
  printf("%d",printRawData(text,strlen(text),1));
  _gettch();
  return 0;
}


否则,您应该使用 Boyer-Moore 之类的快速搜索.
问候.


Otherwise you should use a fast search like Boyer-Moore.
Regards.


这篇关于数一词的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆