C语言 如何在C编程中反转字符串中的单词而不是字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18297563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:12:45  来源:igfitidea点击:

how to reverse the words in a string but not characters in C Programming

c

提问by Mahi

I have this program

我有这个程序

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char text[30];
    int i,j,n;
    puts("Enter the Text:");
    gets(text);
    n=strlen(text);
    for(i=n;i>=0;i--)
    {
       if(text[i-1]==' '||text[i-1]==NULL )
       {
           for(j=i;text[j]!=' ';j++)
           {
               printf("%c",text[j]);
           }
       }

       printf(" ");


    }

    getche();
}

Suppose if i input is "I am Happy" then my output is "Happy am I"

假设如果我输入的是“我很高兴”,那么我的输出是“我很高兴”

I am not sure where i went wrong in this program, i am not getting all the words , I am getting result as "happy [=w am " .Please programmers help me.

我不确定我在这个程序中哪里出错了,我没有得到所有的话,我得到的结果是“快乐 [= w am”。请程序员帮助我。

Thanks In Advance.

提前致谢。

i have found the answer , thanks for your helps, Below is my code

我找到了答案,感谢您的帮助,以下是我的代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char text[100];
    int i,j;
    puts("Enter the Text:");
    gets(text);
    strrev(text);
    for(i=0;text[i]!='
#include <stdio.h>
#include <string.h>

void print_upto_space(const char *s)
{
    do {
        putc(*s, stdout);
    } while (*s++ != ' ');
}

void reverse_print(const char *s)
{
    const char *p = strchr(s, ' ');
    if (p == NULL) { 
        printf("%s ", s);
    } else {
        reverse_print(p + 1);
        print_upto_space(s);
    }
}
';i++) { if(text[i+1]==' ' || text[i+1]==NULL) { for(j=i;j>=0 && text[j]!=' ';j--) printf("%c",text[j]); } printf(" "); } getche(); }

回答by

Basically md5's solution, implemented using recursion:

基本上是 md5 的解决方案,使用递归实现:

void reverseWords( char * str )
{
    int i = 0, j = 0;
    reverseString( str, strlen(str) ); 
    while( 1 ) // Loop forever
    {
        if( *(str+j) == ' ' || *(str+j) == '##代码##') 
        {
            reverseString( str+i, j-i );
            i = j+1;
        }
        if( *(str+j) == '##代码##')
        {
            break;
        }
        j++;
    }
} 
void reverseString(char* str, int len)
{
    int i, j;
    char temp;
    i=j=temp=0;

    j=len-1;
    for (i=0; i<j; i++, j--)
    {
        temp=str[i];
        str[i]=str[j];
        str[j]=temp;
    }
}

回答by md5

You can use the following algorithm:

您可以使用以下算法:

  1. Split the string into words in an array of strings (let's say A).
  2. Print Ain reverse order.
  1. 将字符串拆分为字符串数组中的单词(比方说A)。
  2. A逆序打印。

Although this is O(n)in space, this is perhaps the easier algorithm (since your string is only 30 characters long, it doesn't matter).

尽管这是O(n)在空间中,但这可能是更简单的算法(因为您的字符串只有 30 个字符长,所以没关系)。

回答by Vorac

This solution does not need any extra memory buffers.

此解决方案不需要任何额外的内存缓冲区。

Reverse first the whole string, using two pointers.

首先使用两个指针反转整个字符串。

Then walk the string and reverse each word, using another two pointers.

然后使用另外两个指针遍历字符串并反转每个单词。

回答by Rahul Tripathi

You can try this to reverse the string:-

你可以试试这个来反转字符串:-

##代码##