簡體   English   中英

排序字符串並刪除字符

[英]Sorting a string and removing characters

#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”。 我希望獲得一些技巧,以了解需要解決的代碼的哪些部分。

您可以簡單地使用以下代碼對字符串進行排序,然后使用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;
}

我也想指出

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

if條件不覆蓋其中的列表行,並且對於每個特殊字符read(&,%,/),您都將它們附加到newStr上,因此,您的輸出中將獲得附加的F。您必須執行類似的操作:

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

它將檢查您的字符是否為數字,並且僅在滿足條件時附加。

這是如何編寫程序以使用標准庫的另一種看法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM