2016-04-14 84 views
0

我有一個問題,我的代碼不能夠大寫第一個字符串後的任何字符串的第一個字母,並且不顯示字符串的最後一個字。該代碼使用linux重定向從輸入文件中讀取一系列標題。每個單詞的第一個字母需要大寫,其餘的都是小寫,每個單詞需要用一個黑色空格分隔。這是測試數據的輸出:隱形字符出現在字符串的新行

The 5th 
the Cat In The 
one Fish Two Fish Red Fish Blue 

還要注意如何存在和第5和魚和紅之間的兩個空格

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

void reform (string& imup); 
int main() 
{ 
    string word; 
    string temp = " "; 
    string letters; 
    getline(cin,letters); 
    int length; 
    length = 0; 
    word = ""; 
    while (cin) 
    { 
     length = letters.size(); 
     for (int i =0; i < length; i++) 
     { 

      if (letters[i] != ' ' && letters[i] != '\n') 
      { 
       word = word + letters[i]; 
      } 
      else if ((letters[i] == ' ' && isalpha(letters[i-1])) || 
        (letters[i] == ' ' && isalpha(letters[i+1]))) 
      { 
       reform(word); 
       cout<< word <<" " ; 
       word = ""; 
      } 
      else if ((letters[i] == ' ' && isdigit(letters[i-1])) || 
        (letters[i] == ' ' && isdigit(letters[i+1]))) 
      { 
       reform(word); 
       cout<< word <<" " ; 
       word = ""; 
      } 

     } 
     cout<< endl; 
     getline(cin,letters); 
    } 

    return 0; 
} 


void reform (string& imup) 
{ 
    int size; 
    // cout <<"hi "; 
    size = imup.length(); 
    imup[0] = toupper(imup[0]); 
    for (int i=1; i < size; i++) 
     imup[i] = tolower(imup[i]); 
} 
+1

請編輯您的帖子,並正確縮進代碼。隨機縮進使得其邏輯難以遵循。 –

+0

(不相關)使用'while(getline(..))'而不是兩次調用'getline'。 –

+0

您可以將isalpha和isdigit的測試合併到||中。 (這樣你就不需要重複合併)。如果一行的第一個字符是空格,這將炸燬。如果空格之前的字符既不是字母也不是數字(如$,@或*),會發生什麼情況。 –

回答

0

鑑於a__b作爲輸入(帶下劃線代表一個空白),這會在a後面的空白處識別單詞結尾,打印a_。然後,它將在b之前的空白處檢測字尾,因爲字是空的,所以打印_

我不知道你爲什麼看後的一個空格。在檢查前一個角色之前,你還應該確保我不是0。

爲什麼不直接輸出單詞,如果它在輸入空白或輸入結束時是非空的?

是這樣的:

while (getline(cin, letters)) { 
    bool first = true; 
    string word; 
    for (string::const_iterator s = letters.begin(); s != letters.end(); ++s) { 
    if (*s == ' ' || *s == '\r' || *s == '\n') { 
     if (!word.empty()) { 
     if (!first) cout << ' '; 
     reform(word); 
     cout << word; 
     word = ""; 
     first = false; 
     } 
    } 
    else 
     word += *s; 
    } 
    if (!word.empty()) { 
    if (!first) cout << ' '; 
    reform(word); 
    cout << word; 
    } 
    cout << endl; 
} 

第一標誌,那麼避免了印刷的後空白上的線。

+0

我不確定你的意思,你能提供一個例子嗎?謝謝 – yasky

+0

1.剝離換行符(如果存在)。然後'for(char l:letters)if(c!='')word + = l; else if(word.size()> 0){output word}; if(word.size()!= 0 {output word}; cout << endl;'(第二個if在for循環之外) –

+0

您可能需要將「if(!word.empty())」和相關代碼合併成一個小函數 –

0

這是我的版本代碼的(你可以用迭代器取代循環):

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

int main() 
{ 
     string word; 
     string temp = " "; 
     string letters; 
     int i, len; 
     while(getline(cin,letters)) 
     { 
      len = letters.size(); 
      for(i = 0; (i < len) && isspace(letters[i]); i++) 
       ; 
      if(i >= len) 
       cout << "Empty line is entered" << endl; 
      else 
      { 
       string out_string = letters.substr(i); 
       out_string.replace(0, 1, 1, std::toupper(out_string[0])); 
       for(i = 1; i < len; i++) 
        out_string.replace(i, 1, 1, std::tolower(out_string[i])); 
       cout << out_string << endl; 
      } 
     } 
    }