本文将分享计算字符串中一个单词的出现次数的详细内容,并且还将对计算字符串中一个单词的出现次数的方法进行详尽解释,此外,我们还将为大家带来关于c–查找给定字符串中所有子字符串的出现次数、C#程序计算字符
本文将分享计算字符串中一个单词的出现次数的详细内容,并且还将对计算字符串中一个单词的出现次数的方法进行详尽解释,此外,我们还将为大家带来关于c – 查找给定字符串中所有子字符串的出现次数、C# 程序计算字符串中某个单词的出现次数、C#计算字符串中单词的出现次数、C语言实现计算字符串中最后一个单词长度,单词以空格隔开的相关知识,希望对你有所帮助。
本文目录一览:- 计算字符串中一个单词的出现次数(计算字符串中一个单词的出现次数的方法)
- c – 查找给定字符串中所有子字符串的出现次数
- C# 程序计算字符串中某个单词的出现次数
- C#计算字符串中单词的出现次数
- C语言实现计算字符串中最后一个单词长度,单词以空格隔开
计算字符串中一个单词的出现次数(计算字符串中一个单词的出现次数的方法)
我是Java字符串的新手,问题是我想计算字符串中特定单词的出现次数。假设我的字符串是:
i have a male cat. the color of male cat is Black
现在,我也不想拆分它,所以我想搜索一个“雄猫”这个词。它在我的字符串中出现了两次!
我正在尝试的是:
int c = 0;for (int j = 0; j < text.length(); j++) { if (text.contains("male cat")) { c += 1; }}System.out.println("counter=" + c);
它给了我46个计数器的价值!那么解决方案是什么?
答案1
小编典典您可以使用以下代码:
String in = "i have a male cat. the color of male cat is Black";int i = 0;Pattern p = Pattern.compile("male cat");Matcher m = p.matcher( in );while (m.find()) { i++;}System.out.println(i); // Prints 2
演示版
它能做什么?
它匹配"male cat"
。
while(m.find())
表示在m
找到匹配项时执行循环内给出的任何操作。并且我将i
by 的值递增i++
,因此很显然,这给出了male cat
一个字符串的数量。
c – 查找给定字符串中所有子字符串的出现次数
for(int i=0;i<24;i++) { if(strstr(text,ops[i])) { op++; } }
但我想找到程序中所有子字符串的出现.我可以在JAVA中轻松完成.但我被要求在C中这样做.有帮助吗?
解决方法
#include <string> #include <iostream> using namespace std; int main() { string s("hello hello"); int count = 0; size_t nPos = s.find("hello",0); // first occurrence while(nPos != string::npos) { count++; nPos = s.find("hello",nPos + 1); } cout << count; };
C# 程序计算字符串中某个单词的出现次数
首先设置字符串 -
string str = "Hello World! Hello!";
现在检查字符串中是否出现单词“Hello”并循环 -
while ((a = str1.IndexOf(pattern, a)) != -1) { a += pattern.Length; count++; }
示例
您可以尝试运行以下代码来计算字符串中单词的出现次数。
实时演示
using System; class Program { static void Main() { string str = "Hello World! Hello!"; Console.WriteLine("Occurrence:"+Check.CheckOccurrences(str, "Hello")); } } public static class Check { public static int CheckOccurrences(string str1, string pattern) { int count = 0; int a = 0; while ((a = str1.IndexOf(pattern, a)) != -1) { a += pattern.Length; count++; } return count; } }
输出
Occurrence:2
以上就是C# 程序计算字符串中某个单词的出现次数的详细内容,更多请关注php中文网其它相关文章!
C#计算字符串中单词的出现次数
using System;
class Program {
static void Main() {
string str = Hello World! Hello!;
Console.WriteLine(Occurrence:+Check.CheckOccurrences(str, Hello));
}
}
public static class Check {
public static int CheckOccurrences(string str1, string pattern) {
int count = 0;
int a = 0;
while ((a = str1.IndexOf(pattern, a)) != -1) {
a += pattern.Length;
count++;
}
return count;
}
}
C语言实现计算字符串中最后一个单词长度,单词以空格隔开
计算字符串中最后一个单词长度,单词以空格隔开
//计算字符串最后一个单词的长度,单词以空格隔开。
#include<stdio.h>
#include<string.h>
int main()
{
char str[5000];
gets(str);
int count=0;
int len=strlen(str);
for(int i=(len-1);i>=0;i--)
{
if(str[i]!='' '')
{
count++;
}
else
{
break;
}
}
printf("%d\n",count); return 0;
}
此代码虽然能运行,但是此函数有一个bug,gets函数:没有指定输入字符的大小,限制输入缓冲区的大小,如果输入的字符大于定义的数组长度,会发生内存越界,堆栈溢出。后果非常怕怕
修改代码如下:
//计算字符串最后一个单词的长度,单词以空格隔开。
#include<stdio.h>
#include<string.h>
int main()
{
char str[5000];
fgets(str,5000,stdin);
int count=0;
int len=strlen(str);
for(int i=(len-2);i>=0;i--)
{
if(str[i]!='' '')
{
count++;
}
else
{
break;
}
}
printf("%d\n",count);
return 0;
}
fgets函数会指定大小,如果超出数组大小,会自动根据定义数组的长度截断。
还有一点,上面代码中标红部分
for(int i=(len-2);i>=0;i--)
为什么i = (len -2)
因为fgets函数本身会把回车换行符存入了字符串里。所以实际读入的字符串会多一个回车换行符,故
i=(len-2)
今天关于计算字符串中一个单词的出现次数和计算字符串中一个单词的出现次数的方法的介绍到此结束,谢谢您的阅读,有关c – 查找给定字符串中所有子字符串的出现次数、C# 程序计算字符串中某个单词的出现次数、C#计算字符串中单词的出现次数、C语言实现计算字符串中最后一个单词长度,单词以空格隔开等更多相关知识的信息可以在本站进行查询。
本文标签: