2013-03-04 129 views
3

讓我用更多的細節來解釋我的問題,例如使用示例。用空格作爲單詞間的分隔符移動單詞中的單詞

給定一個字符串:

如果我們想移動的第四個單詞,這是「Assonance的相同元音的在開始時重申的幾個連續字 」 重複,到第二個字的位置,字符串將變爲:

「Assonance rei teration是給定一個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

這不是一個作業。其實這是一個面試問題。我有一個算法。基本思路是:

(1)使用word_begin_indword_end_ind複製目標詞。

(2)從target_begin_indword_begin_ind - 1,將每個字符移到正確的位置。例如,將word_begin_ind-1移動到'word_end_ind',word_begin_ind-2移動到'word_end_ind-1',依此類推。 (3)最後,將副本移動到正確的位置(從target_begin_ind開始)。

我希望每個人都能明白我在問什麼。

你不需要用c來完成這項工作。 C++也是受歡迎的。

任何人都可以幫我找到其他解決方案嗎?

+1

它不不管是否作業,因爲該標籤已被棄用 – Default 2013-03-04 22:57:56

+0

什麼樣的標籤適合這個問題? – Fihop 2013-03-04 23:00:10

+0

算法或許?操作浮現在腦海中。那些你有工作很好的 – Default 2013-03-04 23:01:17

回答

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. 拆分此範圍內成WORD和一切,其他:

    "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. 所以,你所做的一切。

+0

經典面試:) – SomeWittyUsername 2013-03-04 23:08:13

0

我會解決這個問題這樣的:

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

現在我們知道,在目標位置一定的空間,必須進行把背單詞的,所以我想移動n個字節從目標位置向後或向前,依靠,如果原詞是目標位置之前或之後,應與的memmove在目標位置完成的,然後就的memcpy

相關問題