2015-11-29 67 views
-1

searchingWrite函數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; 


} 
+1

當創建一個[最小,完整,可驗證例子](http://stackoverflow.com/help/mcve),如果它實際*編譯*(除非你詢問構建錯誤),它通常是一個加號。不要只是將代碼重新輸入到問題中。請將其複製並粘貼。 –

+0

至於你的問題,試着先在紙上解決它。在紙上寫下輸入字符串,並試圖找出如何計算連續字符。 –

+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. 
+0

謝謝Sudhanshu但我的班級從未學過#include 我們只學過#include 。另外,如果我輸入一個字符串像「布什」,沒有連續的信件,我應該得到一個消息,說沒有連續的字母,所以我應該包括一個if else語句,然後正確? – john315

+1

在C++中,你應該包含''頭文件中的標準C函數,並將它們放在'std ::'中。 – Quentin

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

+0

感謝的人,但這是我的先進方式,即時通訊只介紹到c + +一半像汽車和鄰居這樣的東西::發現從來沒有聽說過他們,但無論如何感謝它 – john315

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

,「你好,世界!」根本不是我想要補充的:P:P:D – nullpointer

相關問題