📌  相关文章
📜  C程序反转给定字符串中的单词

📅  最后修改于: 2022-05-13 01:57:07.535000             🧑  作者: Mango

C程序反转给定字符串中的单词

示例:让输入字符串为“我非常喜欢这个程序”。该函数应将字符串更改为“much very program this like i”

反话

例子

算法

  • 最初,将给定字符串的单个单词逐个反转,对于上面的示例,在反转单个单词之后,字符串应该是“i ekil siht margorp yrev hcum”。
  • 在上面的示例中,从头到尾反转整个字符串以获得所需的输出“much very program this like i”。

下面是上述方法的实现:

C
// C program to reverse a string
#include 
  
// Function to reverse any sequence
// starting with pointer begin and
// ending with pointer end
void reverse(char* begin, 
             char* end)
{
    char temp;
    while (begin < end) 
    {
        temp = *begin;
        *begin++ = *end;
        *end-- = temp;
    }
}
  
// Function to reverse words
void reverseWords(char* s)
{
    char* word_begin = s;
  
    // Word boundary
    char* temp = s;
  
    // Reversing individual words as
    // explained in the first step
    while (*temp) 
    {
        temp++;
        if (*temp == '') 
        {
            reverse(word_begin, 
                    temp - 1);
        }
        else if (*temp == ' ') 
        {
            reverse(word_begin, 
                    temp - 1);
            word_begin = temp + 1;
        }
    }
  
    // Reverse the entire string
    reverse(s, temp - 1);
}
  
// Driver Code
int main()
{
    char s[] = 
    "i like this program very much";
    char* temp = s;
    reverseWords(s);
    printf("%s", s);
    return 0;
}


C
// C program to implement
// the above approach
void reverseWords(char* s)
{
    char* word_begin = NULL;
    
    // temp is for word boundary 
    char* temp = s;
  
    // STEP 1 of the above algorithm
    while (*temp) 
    {
        /*This condition is to make sure 
          that the string start with valid 
          character (not space) only*/
        if ((word_begin == NULL) && 
            (*temp != ' ')) 
        {
            word_begin = temp;
        }
        if (word_begin && 
           ((*(temp + 1) == ' ') ||
            (*(temp + 1) == ''))) 
        {
            reverse(word_begin, temp);
            word_begin = NULL;
        }
        temp++;
    // End of while 
    } 
  
    // STEP 2 of the above algorithm 
    reverse(s, temp - 1);
}


输出:

much very program this like i

上面的代码不处理字符串以空格开头的情况。以下版本处理了这种特定情况,并且在中间有多个空格的情况下不会对 reverse函数进行不必要的调用。感谢 rka143 提供这个版本。

C

// C program to implement
// the above approach
void reverseWords(char* s)
{
    char* word_begin = NULL;
    
    // temp is for word boundary 
    char* temp = s;
  
    // STEP 1 of the above algorithm
    while (*temp) 
    {
        /*This condition is to make sure 
          that the string start with valid 
          character (not space) only*/
        if ((word_begin == NULL) && 
            (*temp != ' ')) 
        {
            word_begin = temp;
        }
        if (word_begin && 
           ((*(temp + 1) == ' ') ||
            (*(temp + 1) == ''))) 
        {
            reverse(word_begin, temp);
            word_begin = NULL;
        }
        temp++;
    // End of while 
    } 
  
    // STEP 2 of the above algorithm 
    reverse(s, temp - 1);
}

时间复杂度: O(n)
另一种方法:

有关详细信息,请参阅有关给定字符串中的反向单词的完整文章!