2012-11-26 133 views
0

我想添加'。'字符除了字符串中的另一個字符,但我不知道該怎麼做?可能嗎?如何將點字符添加到字符串中的字符?

#include <iostream> 

#include <string.h> 

using namespace std; 

int main(int argc, char *argv[]) { 

    string input; 
    char dot='.'; 
    cin>>input; 
    for(int i=0;i<input.length();i++) 
    { 

     if(input[i]>=65 && input[i]<=90) 
       { 
        input[i]=input[i]+32; 
       } 
     if((input[i]=='a') || (input[i]=='e') || (input[i]=='i') || (input[i]=='o') || input[i]=='y' || input[i]=='u') 
     { 
      input.erase(i,i+1); 
     } 
     input[i]+=dot; 
    } 
    cout<<input<<endl; 
    return 0; 
} 
+1

你究竟在做什麼?我認爲你正在使用'std :: string :: erase()'錯誤的方式。例如,如果元音位於索引4,那麼您將從索引4開始擦除5個字符。這是您真正想要的嗎?或者你只是想自己擦除元音?你想讓這個點完全出現在哪裏?你想用點替換元音還是在元音旁邊放點?您使用'[]'運算符使用'erase()'的方式,可能會出現越界錯誤。你需要清除你的問題。 –

+0

我的任務是刪除所有的元音,然後在任何剩餘的字符之前放一個點 – SUE

+0

替換你的if(if(input [i]> = 65 && input [i] <= 90) input [i] = input [i] +32; })''input'[i] = tolower(input [i])''。你認爲ASCII正在使用,這不能保證,再加上它更短,更可讀。 – chris

回答

0

根據您的意見,這聽起來像你想是這樣的:

#include <iostream> 
#include <string> 
#include <algorithm> 

int main(int argc, char *argv[]) 
{ 
    std::string input; 
    std::cin >> input; 

    std::transform (input.begin(), input.end(), input.begin(), tolower); 

    size_t i = 0; 
    while (i < input.length()) 
    { 
     switch (input[i]) 
     { 
      case 'a': 
      case 'e': 
      case 'i': 
      case 'o': 
      case 'y': 
      case 'u': 
      { 
       size_t pos = input.find_first_not_of("aeioyu", i+1); 
       if (pos == std::string::npos) 
        pos = input.length(); 
       input.erase(i, pos-i); 
       break; 
      } 

      default: 
      { 
       input.insert(i, '.'); 
       i += 2; 
       break; 
      } 
     } 
    } 

    std::cout << input << std::endl; 
    return 0; 
} 
0

從cpluplus.com引用(http://www.cplusplus.com/reference/string/string/insert/

// inserting into a string 
#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    string str="to be question"; 
    string str2="the "; 
    string str3="or not to be"; 
    string::iterator it; 

    // used in the same order as described above: 
    str.insert(6,str2);     // to be (the)question 
    str.insert(6,str3,3,4);    // to be (not)the question 
    str.insert(10,"that is cool",8); // to be not (that is)the question 
    str.insert(10,"to be ");   // to be not (to be)that is the question 
    str.insert(15,1,':');    // to be not to be(:) that is the question 
    it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question 
    str.insert (str.end(),3,'.');  // to be, not to be: that is the question(...) 
    str.insert (it+2,str3.begin(),str3.begin()+3); // (or) 

    cout << str << endl; 
    return 0; 
} 

此外,檢查這些鏈接:

http://www.cplusplus.com/reference/string/string/ http:///www.cplusplus.com/reference/string/string/append/ http://www.cplusplus.com/reference/string/string/push_back/

0

你嘗試寫代碼之前,你應該寫的什麼是應該做了詳細的 規範。使用你的代碼,我只能猜測:轉換爲小寫(天真地假裝 你只會遇到26個ASCII字符串),然後 刪除所有元音(再次,非常天真,因爲確定 是否是某種東西一個元音或不是不重要的,即使在 英文—考慮y在今天和日期),最後 在每個字符後面插入一個點。的 這樣做,最明顯的方法是這樣的:

std::string results; 
for (std::string::const_iterator current = input.begin(), 
       end = input.end(); 
     current != end; 
     ++ current) { 
    static std::string const vowels("aeiouAEIOU"); 
    if (std::find(vowels.begin(), vowels.end(), *current) 
       != vowels.end()) { 
     results.push_back(
      tolower(static_cast<unsigned char>(*current))); 
    } 
    results.push_back('.'); 
} 

但同樣,我只是猜測,以你正在嘗試做的。

另一種方法是在 初始字符串上使用std::transform使其全部小寫。如果你經常做這種事情,你會有一個ToLower功能 對象;否則,編寫 可能太費神,只能使用std::transform一次。

0

我假設你想要這個輸入:

Hello world! 

爲了給你這樣的輸出:

h.ll. w.rld! 

而不是試圖修改到位的字符串,你可以簡單地產生一個新的字符串當您去:

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

int main(int argc, char *argv[]) { 
    string input; 
    getline(cin, input); 
    string output; 
    const string vowels = "aeiouy"; 
    for (int i = 0; i < input.size(); ++i) { 
     const char c = tolower(input[i]); 
     if (vowels.find(c) != string::npos) { 
      output += '.'; 
     } else { 
      output += c; 
     } 
    } 
    cout << output << '\n'; 
    return 0; 
} 

注:

  • <cctype>用於toupper()

  • <string.h>已棄用;使用<string>

  • 閱讀整線,getline(); istream::operator>>()讀取單詞。

  • 使用tolower()toupper(),& c。用於角色轉換。 c + 32沒有描述你的意圖。

  • 當您需要比較時,c >= 'A' && c <= 'Z'將起作用;你不需要使用ASCII碼。

  • 使用const表示不會改變的事物。