繁体   English   中英

在一个字符串中移动一个单词,其中空格作为单词之间的分隔符

[英]move a word in a string with spaces as delimiters between words

让我用一个例子更详细地解释我的问题。

给定一个字符串:

“在几个连续的单词开头,辅音是重复相同的元音”

如果我们想将第四个单词( 重复单词)移动到第二个单词的位置,则该字符串将变为:

“辅音重复是几个连续单词开头的同一个元音”

鉴于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. 从一个位置的开始到另一个位置的结束之间的范围:

     "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. 所以你已经完成了。

我会像那样解决它:

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

现在我们知道在目标位置必须留出一些空间来重新放入单词,所以我想要来回移动目标位置的n个字节,具体取决于原始单词是在目标位置之前还是之后,这应该用memmove完成,然后只记住目标位置的单词

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM