如何编写一个函数来计算字符串中相同的连续字母
How do I write a function that counts consecutive letters in a string that are identical
searching编写一个函数consclets,该函数将接收一个字符串作为参数。函数将确定字符串中两个或多个连续字母相同的所有情况。
例如,如果"Barrymore"被发送到函数,它会说字符串中有连续的字母r和o。但是,如果"布什"被发送到功能,它会说没有两个连续的字母是相同的。
这是我的代码,问题是当我输入一封信来找到它时,它找到了它,但不是连续的
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char searching=' ';
string name=" ";
int counter =0;
cout<<"Enter a name : "<<endl;
getline(cin, name);
cout<<"Which letter would you like to count the number of times it appears: "<<endl;
cin>>name;
for(int i=0; i<name.length();i++){
if(sentence[i]==searching){
counter++;
}
}
cout<<"The letter " << searching << " appears "<< counter << " times ";
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main(int argc, char *argv[]) {
char a[30];
int i,c=0;
printf("enter a stringn");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]==a[i+1])
{
printf("%c is consecutiven",a[i]);
c++;
}
}
if(c==0)
{
printf("No consecutive letters");
}
return 0;
}
// Check this. This is proper code for your problem.
这里是一个使用标准算法库的实现。我把"run"称为相同的连续字母序列。该算法不一定是最优的,但它很简单,这也很重要。
void conseclets(std::string const &str) {
// Keep the end of the string, and point i to the first run's beginning
auto e = end(str), i = std::adjacent_find(begin(str), e);
if(i == e)
std::cout << "No repetition.n";
else do {
// Locate the end of the run (that is, the first different letter)
auto next = std::find_if(i, e, [&i](auto const &c){ return c != *i; });
// Print out the match
std::cout << "Letter " << *i << " is repeated "
<< std::distance(i, next) << " times.n";
// Skip to the next run's beginning
i = std::adjacent_find(next, e);
// Do so until we reached the end of the string
} while(i != e);
}
在Coliru 上直播
尽管有几种方法可以做到这一点,但只需优化修改即可实现此处所需的功能:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
char searching = ' ';
string name = " ";
int counter = 0;
cout << "Enter a name : " << endl;
getline(cin, name);
cout << "Which letter would you like to count the number of times it appears: " << endl;
cin >> searching;
for (int i = 0; i < name.length(); i++) {
if (name[i] == searching) {
counter++;
}
}
cout << "The letter " << searching << " appears " << counter << " times ";
return 0;
}
#include <iostream>
#include <iterator>
using namespace std;
template<typename I, typename O> O repeats(I b, I e, O result){
while(b!=e){
bool once = true;
I p = b;
while(++b!=e && *p==*b){
if(once){
*result++ = *p;
once = false;
}
}
}
return result;
}
int main() {
string name = "Barrymoore";
string res;
repeats(begin(name), end(name), back_inserter(res));
cout << "There are " << res.size() << " consecutive letters :" << res << endl;
return 0;
}
相关文章:
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 检查连续对的字符串C++
- 不带连续 1 的位字符串,使用自上而下的 DP
- 使用 fprintf 连续写入字符串
- 将字符串分为连续的非空格字符的序列
- "map"容器C++是否对字符串的连续子字符串应用 Rabin-Karp 算法?
- 连续字符串输入的C 动态数组
- 从带有位偏移量的std::字符串中按字节提取连续位
- 从一个字符串中获取 2-5 个连续的单词短语,我得到了 2 个工作,但在做 3 个时遇到麻烦
- 删除字符串数组中的所有连续重复项
- 将字符串重新排序为没有连续相同字符的字符串
- 静态 c++ 对象中的 JNI 环境指针并调用连续两次使用字符串参数的 Java 函数会使 JVM 崩溃
- C++,转换字符串,使连续下划线序列变为单个下划线
- 删除字符串中的连续重复值
- 使用 std::cin 的连续字符串输入
- 从字符串中检索数据并连续写入数据库
- 如何编写一个函数来计算字符串中相同的连续字母
- 尽量减少映射<字符串、字符串的连续相等提取次数>
- 在连续内存中存储相同长度的字符串
- 重用字符串流来连续解析多个字符串的值