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

时间:2015-11-29 17:04:16

标签: c++

searchWrite一个函数 conseclets ,它将接收一个字符串作为参数。该函数将确定字符串中两个或多个连续字母相同的所有情况。

例如,如果将“Barrymoore”发送到函数,则会说字符串中有连续的字母r和o。但如果将“布什”发送给该函数,则会说没有两个连续的字母是相同的。

这是我的代码问题,当我写信找到它找到但不连续

#include <iostream>
#include <string>
using namespace std;

int main ()

{
    char searching='\0';
    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;


}

4 个答案:

答案 0 :(得分: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 string\n");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]==a[i+1])
{
    printf("%c is consecutive\n",a[i]);
    c++;
}
}
if(c==0)
{
printf("No consecutive letters");   
}
return 0;
}
// Check this. This is proper code for your problem.

答案 1 :(得分:0)

这是一个利用标准算法库的实现。我称之为“运行”相同连续字母的序列。算法不一定是最优的,但它很简单,也很重要。

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

Live on Coliru

答案 2 :(得分: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;
}

答案 3 :(得分:0)

虽然有几种方法可以做到,但只需最佳地修改你的方法来实现这里所需要的:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    char searching = '\0';
    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;
}