3

例を使用して、私の質問をより詳細に説明しましょう。

与えられた文字列:

「類韻とは、いくつかの連続した単語の最初に同じ母音が繰り返されることです」

reiterationである 4 番目の単語を2 番目の単語の位置に移動する場合、文字列は次のようになります。

「類韻反復は、いくつかの連続する単語の先頭にある同じ母音のことです」

C次のようにプロトタイプ化された関数が与えられた場合:

void move(char *s, int word_begin_ind, int word_end_ind, int target_begin_ind)

仕事をするためにこの機能を実装する方法は?

上記の例では、word_begin_ind = 17, word_end_ind = 27, target_begin_ind = 10

これは宿題ではありません。実は面接の質問です。私はアルゴリズムを持っています。基本的な考え方は次のようなものです。

word_begin_ind(1) と を使用してターゲット単語のコピーを作成しますword_end_ind

(2) からtarget_begin_indまでword_begin_ind - 1、すべての文字を正しい位置に移動します。たとえば、word_begin_ind-1「word_end_ind」、word_begin_ind-2「word_end_ind-1」などに移動します。

(3) 最後に、コピーを正しい位置 (target_begin_ind から開始) に移動します。

私が求めていることを誰もが理解できることを願っています。

この作業を行うために c を使用する必要はありません。C++も歓迎です。

他の解決策を見つけるのを手伝ってくれる人はいますか?

4

2 に答える 2

8
  1. ある位置の開始点と他の位置の終了点の間の範囲を取る:

    "Assonance [is the reiteration] of the same vowel sound at the beginning of several consecutive words"
    
  2. この範囲を逆にします。

    "Assonance [noitaretier eht si] of the same vowel sound at the beginning of several consecutive words"
    
  3. この範囲を単語とその他すべてに分割します。

    "Assonance [noitaretier|eht si] of the same vowel sound at the beginning of several consecutive words"
    
  4. 逆の言葉:

    "Assonance [reiteration|eht si] of the same vowel sound at the beginning of several consecutive words"
    
  5. すべてを逆にする-その他:

    "Assonance [reiteration|is the] of the same vowel sound at the beginning of several consecutive words"
    
  6. これで完了です。

于 2013-03-04T23:06:16.140 に答える
0

私はそれを次のように解決します:

char * temp = malloc(end-begin);
memcpy(temp,s,end-begin);

これで、宛先の場所に単語を元に戻すためのスペースを作成する必要があることがわかったので、元の単語が宛先の場所の前か後かに応じて、宛先の場所から n バイト前後に移動したいと思います。 、これはmemmoveで行う必要があります。次に、宛先の場所で単語をmemcpyするだけです

于 2013-03-04T23:19:09.990 に答える