簡體   English   中英

將char數組拆分為其他數組以供以后使用並忽略數字c ++

[英]Splitting char array into other arrays for using it later and ignoring numbers c++

因此,我需要進行此分配,其中您必須通過給定的重新放置數量來重新定位char數組中的字母。 最后一個字母必須成為第一個。 例如:

輸入:Hello 3輸出:lloHe

但是,如果您有一個句子,則必須對每個單詞分別進行處理,而且,如果有數字,則必須忽略它們。 所以我在處理數字檢查和處理單獨的單詞時遇到麻煩(我使用strtok拆分它們)。 這是我到目前為止的內容:

#include <iostream>
#include <cstring>

using namespace std;


void Reposition(char text[10000], int n, char result[10000])
{
    int startIndex = strlen(text)-1;
    int k = n-1;

    int currentIndex = 0;
    for(int i = 0; i < n; i++)
    {
        result[k] = text[startIndex];
        k--;
        startIndex--;
        currentIndex++;

    }

    for(int i = 0; i <= startIndex; i++)
    {
        result[currentIndex] = text[i];
        currentIndex++;
    }
}


int main()
{
    char text[10000];
    cin.getline(text,10000);
    int n;
    cin >> n;
    char result[10000];

    char *words;
    words = strtok(text, " .,");

    while(words != NULL)
    {
        Reposition(text, n, result);
        words = strtok(NULL, " .,");

    }


    for(unsigned i = 0; i <= strlen(result); i++)
       cout << result[i];


    return 0;
}

使用std::string代替C樣式的字符串

要從字符串中刪除數字,請使用<algorithm> std::remove_if

std::string s;
. . .
s.erase(std::remove_if(s.begin(), s.end(), ::isdigit), s.end());

要重新定位字符串中的字符,請使用std::rotate

std::rotate(s.begin(), s.begin() + 1, s.end());

我做了功課 不知道您是否熟悉所有這些代碼。 我還重寫了您的重新定位代碼。 看起來很亂。 嘗試從中學習一些東西。

#include <iostream>
#include <cstring>
#include <ctype.h>

using namespace std;


void Reposition(char * text, int len, int n, char * result)
{
    int k = n - 1;
    for(int i = 0; i < len; i++)
    {
        result[i] = text[k++];
        if(k == len) k = 0;
    }
}


int main()
{
    char text[10000];
    cin.getline(text,10000);
    int n;
    cin >> n;
    char result[10000];

    char * word;
    char * beginOfWord = text;
    char * resultPointer = result;
    int wordLen;

    while(* beginOfWord)
    {
        // copy up to somthing from the alphabet
        if(!isalpha(* beginOfWord))
        {
            *resultPointer++ = * beginOfWord++;
            continue;
        }
        // Find the end of this word
        word = strpbrk(beginOfWord, " .,0123456789");
        if(word != NULL)
        {
            // len is distance between end of word and begin of word
            wordLen = word - beginOfWord;
        }
        else
        {
            // Maybe it is the end of the string
            wordLen = strlen(beginOfWord);
        }
        //reposition the word
        Reposition(beginOfWord, wordLen, n, resultPointer);
        // Move the pointers beyond the word
        beginOfWord += wordLen;
        resultPointer += wordLen;
    }
    //Always terminate
    *resultPointer ='\x0';
    cout << result;
    return 0;
}
//reverse will reverse the string starting at position xn and ending at position (yn-1)
void reverse(char *str, int xn, int yn)
{
        //positioning the pointers appropriately
        char *start = str + xn;
        char *end = str + yn - 1;

        char temp;
        while(start < end)
        {
                temp = *start;
                *start = *end;
                *end = temp;

                ++start;
                --end;
        }
}

//one of the logic to reposition
void reposition(char *str, int n)
{
    int length = strlen(str);

    n = (length > n) ? n : (n % length);

    reverse(str, 0, n);
    reverse(str, n, length);
    reverse(str, 0, length);
}

int main()
{
    char text[10000];
    cin.getline(text,10000);
    int n;
    cin >> n;
    char result[10000];

    strcpy(result, text);

    cout << "before: " << result << endl;

    char *word;
    word = strtok(text, " .,");

    while(word != NULL)
    {
            //check if it is not a number
            if(isdigit(word[0]) == 0)
            {
                            reposition(word, n);

                            //find the word postion in text
                            int word_position = word - text;

                            //copy the repositioned word in result at its corresponding position.
                            int i = 0;
                            while(word[i])
                            {
                                    result[word_position + i] = word[i];
                                    ++i;
                            }
            }

            word = strtok(NULL, " .,");
    }


    cout << "after : " << result << endl;

    return 0;
}

輸出:

abcd 345 pqrst 321
3
before: abcd 345 pqrst 321
after : dabc 345 stpqr 321

暫無
暫無

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

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