如何编写一个函数来计算字符串中相同的连续字母

How do I write a function that counts consecutive letters in a string that are identical

本文关键字:字符串 连续 计算 何编写 函数 一个      更新时间:2023-10-16

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;
}