如何反转字符串中的每个单词?

我被迫不得使用$ kubectl exec -it redis-cluster-set-1-2 -- bash root@redis-cluster-set-1-2:/data# redis-cli GET Australia (error) MOVED 1738 10.12.1.30:6379 root@redis-cluster-set-1-2:/data# redis-cli -h 10.12.1.30 GET Australia "Sydney" 中的任何东西。

我想反转传递的字符串中的每个单词。

这是我到目前为止所拥有的:

<string.h>

我的代码将整个字符串反转,但我希望将单个单词反转-因此,此示例的结果将是#include <stdio.h> char * reversepPrint( char *name ) { char *normal = name,*reverse = name; while ( *reverse ) ++reverse; if ( normal < reverse ) { for ( ; normal < --reverse; ++normal ) { char c = *normal; *normal = *reverse; *reverse = c; } } return name; } int main( void ) { char s[] = "Simon liebt Pizza!"; printf("%s",reversepPrint(s)); return 0; }

girlmimi 回答:如何反转字符串中的每个单词?

我们需要分解成两个问题。对于一个问题(反转字符串),我们已经拥有大多数解决方案;我们只需要使其与子字符串一起使用即可。我们主要通过删除查找字符串结尾的代码来完成此操作:

/* reverse substring [left,right) in-place */
void reverseSubstring(char *left,char *right)
{
    while (left < --right) {
        char c = *right;
        *right = *left;
        *left++ = c;
    }
}

问题的另一半是找到单词之间的边界。我们可以使用isspace()将开始和结束指针定位在正确的位置,并用它们调用我们的reverseSubstring

#include <ctype.h>
char *reversepPrint(char *const name)
{
    char *start = name;
    char *end;

    while (*start) {
        while (*start && isspace(*start)) {
            ++start;
        }
        end = start;
        while (*end && !isspace(*end)) {
            ++end;
        }
        reverseSubstring(start,end);
        start = end;
    }

    return name;
}

如果您还被禁止使用<ctype.h>,则为此功能编写自己的简单isspace()并不困难。


完整程序

/* reverse substring [left,right) in-place */
void reverse_substring(char *left,char *right)
{
    while (left < --right) {
        char c = *right;
        *right = *left;
        *left++ = c;
    }
}

#include <ctype.h>
/* reverse individual words in string */
/* word boundaries determined by isspace() */
char *reverse_words(char *const name)
{
    for (char *start = name,*end;  *start;  start = end) {
        while (*start && isspace(*start)) { ++start; }
        end = start;
        while (*end && !isspace(*end)) { ++end; }
        reverse_substring(start,end);
    }

    return name;
}

#include <stdio.h>
int main(void)
{
    char s[] = "Simon liebt Pizza!";
    printf("%s",reverse_words(s));
}
,

您的函数reversepPrint反转字符串。

由于要按单词反向,因此必须解析字符串才能对每个单词应用函数reversepPrint。为此,您可以使用空格字符作为分隔符。

,

我要做的是以下事情:

  1. 创建一个仅反转字符串的n个字符的函数
  2. 使用它来反转原始数组的单词。
  3. 单词很容易识别,因为它们是非null和非空格字符的块。

类似以下的方法应该起作用(请注意,我没有测试代码)并产生以下输出:nomiS tbeil !azziP

//this is basically your original function
char * reverse_n( char *name,const int  len )
{
    char *normal = name,*reverse = name+len;

    if ( normal < reverse )
    {
        for ( ; normal < --reverse; ++normal  )
        {
            char c = *normal;
            *normal = *reverse;
            *reverse  = c;
        }
    }

    return name;
}

char * my_reverse( char *nname)
{
    char* name=nname;
    while(*name)
    {
        char* next = name;
        int l = 0;
        //find the next word and its length
        while(*next && *next!=' '){
            next++;
            l++;
        }
        //reverse it
        reverse_n(name,l);
        name=next;
        //skip the space
        if(*name)
            name++;
    }
    return nname;
}
,

在不使用 string.h

的情况下,我在逻辑上不做任何努力
#include <stdio.h>

char * reversepPrint( char *name )
{

    char *normal = name,*reverse = name;

    while ( *reverse ) ++reverse;

    if ( normal < reverse )
    {
        for ( ; normal < --reverse; ++normal  )
        {
            char c = *normal;
            *normal = *reverse;
            *reverse  = c;
        }
    }

    return name;
}

int main( void ) 
{
    char s[] = "Simon liebt Pizza!";
    int i;
    int num_of_spaces = 0;

    int length = 0;

    char *temp = &s;

    while(*temp!='\0'){
        temp++;
    }
    length = temp - s;


    for (i = 0; i<length; i++)
    {

        if (s[i]==' ')
        {
            num_of_spaces++;
        }
    }

    char x[num_of_spaces+1][100];
    i = 0;
    int index = 0,index1 = 0,k = 0;
    for(i = 0; i < length; i++)
    {
        if(s[i]!=' ')
        {
            x[k][index] = s[index1];
            index++;

        }else{

            x[k][index] = '\0';
            index = 0;
            k++;

        }
        index1++;
    }

    i = 0;
    for(i = 0; i<=num_of_spaces; i++)
    {
        printf("%s\n",reversepPrint(x[i]));
    }



    return 0;
}

这就是代码的作用

  • 给出任何可以找到其长度的字符串,而无需使用 strlen
  • 然后,代码将查找字符串的总数(它们之间的空格)
  • 之后,我将创建一个二维数组,其尺寸为[strings] [100](每个字符串的长度为100)
  • 将内容分别复制到每个字符串
  • 然后遍历2d数组并调用该方法。
,

这是使用一些嵌套循环的版本:

#include <ctype.h>

char * reversepPrint( char *name )
{
    char *s = name;

    while (*s)
    {
        char *t = s;

        /* Find end of non-space character sequence. */
        while (*t && *t == (unsigned char)*t && !isspace(*t))
        {
            t++;
        }
        if (t - s > 1)
        {
            /* Got a non-space character sequence of length > 1. */
            char *e = t;

            /* Reverse the non-space character sequence. */
            do
            {
                char tmp = *s;

                *s++ = *--e;
                *e = tmp;
            } while (s < e);

            /* Start past non-space characters for next iteration. */
            s = t;
        }
        else
        {
            /* Skip space or singleton non-space. */
            s++;
        }
    }

    return name;
}

在外部s循环的每次迭代中,变量name用于推进while字符串。变量t在外部循环的每次迭代中都初始化为s,然后由内部while (*t && ...)循环经过任何非空格字符。将t移到所有非空格字符之后,非空格字符序列的长度为t - s。 (如果*s是空格字符,则该长度将为0。)如果该长度大于1,则它将使用内部do { ... } while循环来反转非空格字符的序列,然后分配s = t已准备好进行外循环的下一次迭代。否则,*s是空格字符或单例非空格字符,因此s在外循环的下一次迭代中前进一个字符。

,

请改用此逻辑,将字符串的各个单词一一反转,例如,如果字符串是“ i like programming”,则在将单个单词反向后,字符串应为“ i ekil gnimmargorp”。

我希望此代码段对您有所帮助

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* begin = s; 

    char* temp = s;  
    while (*temp) { 
        temp++; 
        if (*temp == '\0') { 
            reverse(begin,temp - 1); 
        } 
        else if (*temp == ' ') { 
            reverse(begin,temp - 1); 
            begin = temp + 1; 
        } 
    } 
} 
本文链接:https://www.f2er.com/2976849.html

大家都在问