2017-07-19 95 views
0
#include <iostream> 
const int SIZE = 100; 

using namespace std; 

int main() 
{ 
    char *pStr, str[SIZE] = "", newStr[SIZE] = "", ch; 
    int count = 0, i = 0, j = 0; 

    cout << "Enter a number of mixed characters: "; 
    cin.getline(str, SIZE); 
    pStr = str; 

    while (*pStr != '\0') 
    { 
     if (isalnum(*pStr)) 
      ch = toupper(*pStr); 
     newStr[i++] = ch; 

     if (*pStr = ' ') 
      count++; 
     pStr++; 
    } 
    newStr[i] = '\0'; 

    cout << strlen(str) - strlen(newStr) << " characters were filtered out, " 
     << " out of which " << count << " whitespaces were encountered.\n"; 

    int temp; 

    for (i = 0; i < strlen(newStr) - 1; i++); 
    { 
     for (j = i + 1; j < strlen(newStr); j++); 
     { 
      if (newStr[j] < newStr[i]) // sorts in alphabetical 
      {      // and numerical order 
       temp = newStr[i];   
       newStr[i] = newStr[j]; 
       newStr[j] = temp; 
      } 
     } 
    } 

    cout << "New sorted string: " << newStr << endl; 
    return 0; 
} 

我在這裏有一個代碼,應該採取一個輸入字符串,並按特定順序打印出來,並刪除其他字符以及空格。數字和字母應按數字和字母順序排序。所以如果你輸入「khff &%/ 321」,輸出應該是「123FFHK」。排序字符串和刪除字符

但是,當我使用所述輸入字符串嘗試代碼時,我得到的輸出是「KHFFFFFF32」。我希望得到一些關於我需要仔細研究以解決問題的代碼部分的提示。

+1

我建議你閱讀[如何調試小程序(https://ericlippert.com/2014/03/05/how-to-debug-small-programs /)作者:Eric Lippert,並學習如何使用調試器。 –

+1

我還假設這是一個練習指針,數組和C風格的字符串?否則,如果你使用標準的類和算法,它將只是幾行。 –

+0

'str [SIZE] =「」'不符合你的想法。你的排序算法嚴重錯誤。看看'std :: sort','std :: remove_if'和'std :: transform' – IlBeldus

回答

1

我還想指出

if (isalnum(*pStr)) 
    ch = toupper(*pStr); 
    newStr[i++] = ch; 

if條件中不包含每行特殊字符(&,%,/),因此您將在輸出中添加額外的Fs。您必須做類似於:

if (isalnum(*pStr)) 
    { ch = toupper(*pStr); 
    newStr[i++] = ch; 
    } 

它將檢查您的角色是否是alnum,並且只有在滿足條件時纔會附加。

3

您可以簡單地使用這個代碼串排序,你想,然後使用erase功能剝離出非字母數字字符:

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

int main() { 
    std::string word = "khff &%/123"; 
    word.erase(std::remove_if(word.begin(), word.end(), [](char ch){ return !::isalnum(ch); }), word.end()); 
    std::sort(word.begin(), word.end()); 
    std::cout << word << '\n'; 
    return 0; 
} 
+0

爲什麼在使用窄字符時使用寬字符iswalnum?並進行一些優化:首先刪除不需要的字符*然後*排序。 –

+0

@Someprogrammerdude謝謝,修復。 – Annabelle

1

這裏是另一個拿上節目怎麼可能被寫入使用標準庫:

#include <iostream> 
#include <sstream> 
#include <algorithm> 
#include <string> 
#include <cstdio> 

#define USER_TYPES_INPUT // comment this line out to use own test stream 
#ifdef USER_TYPES_INPUT 
auto& my_cin = std::cin; 
#else 
std::stringstream my_cin{R"(khff&%/32 1)"}; 
#endif 

int main() 
{ 
    std::string line; 
    std::cout << "Enter a number of mixed characters: \n"; 
    std::getline(my_cin, line); 
    auto original_size = line.size(); 
    auto space_count = std::count_if(line.begin(), line.end(), [](auto c){return ::isblank(c); }); 
    line.erase(std::remove_if(line.begin(), line.end(), [](char c){ return !::isalnum(c); }), line.end()); 
    std::transform(line.begin(), line.end(), line.begin(), [](auto& c){return ::toupper(c); }); 
    std::sort(line.begin(), line.end()); 
    std::cout << original_size - line.size() << " characters were filtered out, out of which " 
     << space_count << " whitespace" << (space_count == 1 ? " was" : "s were") << " encountered.\n"; 
    std::cout << "New sorted string: " << line << '\n'; 
    return 0; 
}