Reverse the ordering of words in a string

后端 未结 30 3804
青春惊慌失措
青春惊慌失措 2020-11-22 10:23

I have this string s1 = \"My name is X Y Z\" and I want to reverse the order of the words so that s1 = \"Z Y X is name My\".

I can do it u

相关标签:
30条回答
  • 2020-11-22 11:02

    Reverse the entire string, then reverse the letters of each individual word.

    After the first pass the string will be

    s1 = "Z Y X si eman yM"
    

    and after the second pass it will be

    s1 = "Z Y X is name My"
    
    0 讨论(0)
  • 2020-11-22 11:03

    In Python, if you can't use [::-1] or reversed(), here is the simple way:

    def reverse(text):
    
      r_text = text.split(" ")
      res = []
      for word in range(len(r_text) - 1, -1, -1): 
        res.append(r_text[word])
    
      return " ".join(res)
    
    print (reverse("Hello World"))
    
    >> World Hello
    [Finished in 0.1s]
    
    0 讨论(0)
  • 2020-11-22 11:04

    This quick program works..not checks the corner cases though.

    #include <stdio.h>
    #include <stdlib.h>
    struct node
    {
        char word[50];
        struct node *next;
    };
    struct stack
    {
        struct node *top;
    };
    void print (struct stack *stk);
    void func (struct stack **stk, char *str);
    main()
    {
        struct stack *stk = NULL;
        char string[500] = "the sun is yellow and the sky is blue";
        printf("\n%s\n", string);
        func (&stk, string);
        print (stk);
    }
    void func (struct stack **stk, char *str)
    {
        char *p1 = str;
        struct node *new = NULL, *list = NULL;
        int i, j;
        if (*stk == NULL)
        {
            *stk = (struct stack*)malloc(sizeof(struct stack));
            if (*stk == NULL)
                printf("\n####### stack is not allocated #####\n");
            (*stk)->top = NULL;
        }
        i = 0;
        while (*(p1+i) != '\0')
        {
            if (*(p1+i) != ' ')
            {
                new = (struct node*)malloc(sizeof(struct node));
                if (new == NULL)
                    printf("\n####### new is not allocated #####\n");
                j = 0;
                while (*(p1+i) != ' ' && *(p1+i) != '\0')
                {
                    new->word[j] = *(p1 + i);
                    i++;
                    j++;
                }
                new->word[j++] = ' ';
                new->word[j] = '\0';
                new->next = (*stk)->top;
                (*stk)->top = new;
            }
            i++;
       }
    }
    void print (struct stack *stk)
    {
        struct node *tmp = stk->top;
        int i;
        while (tmp != NULL)
        {
            i = 0;
            while (tmp->word[i] != '\0')
            {
                printf ("%c" , tmp->word[i]);
                i++;
            }
            tmp = tmp->next;
        }
        printf("\n");
    }
    
    0 讨论(0)
  • 2020-11-22 11:05

    This is assuming all words are separated by spaces:

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char string[] = "What are you looking at";
        int i, n = strlen(string);
    
        int tail = n-1;
        for(i=n-1;i>=0;i--)
        {
            if(string[i] == ' ' || i == 0)
            {
                int cursor = (i==0? i: i+1);
                while(cursor <= tail)
                    printf("%c", string[cursor++]);
                printf(" ");
                tail = i-1;
            }
        }
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 11:08

    It can be done more simple using sscanf:

    void revertWords(char *s);
    void revertString(char *s, int start, int n);
    void revertWordsInString(char *s);
    
    void revertString(char *s, int start, int end)
    {
         while(start<end)
         {
             char temp = s[start];
             s[start] = s[end];
             s[end]=temp;
             start++;
             end --;
         }
    }
    
    
    void revertWords(char *s)
    {
      int start = 0;
    
      char *temp = (char *)malloc(strlen(s) + 1);
      int numCharacters = 0;
      while(sscanf(&s[start], "%s", temp) !=EOF)
      {
          numCharacters = strlen(temp);
    
          revertString(s, start, start+numCharacters -1);
          start = start+numCharacters + 1;
          if(s[start-1] == 0)
          return;
    
      }
      free (temp);
    
    }
    
    void revertWordsInString(char *s)
    {
       revertString(s,0, strlen(s)-1);
       revertWords(s);
    }
    
    int main()
    {
       char *s= new char [strlen("abc deff gh1 jkl")+1];
       strcpy(s,"abc deff gh1 jkl");
       revertWordsInString(s);
       printf("%s",s);
       return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 11:08
    public class manip{
    
    public static char[] rev(char[] a,int left,int right) {
        char temp;
        for (int i=0;i<(right - left)/2;i++)    {
            temp = a[i + left];
            a[i + left] = a[right -i -1];
            a[right -i -1] = temp;
        }
    
        return a;
    }
    public static void main(String[] args) throws IOException {
    
        String s= "i think this works";
        char[] str = s.toCharArray();       
        int i=0;
        rev(str,i,s.length());
        int j=0;
        while(j < str.length) {
            if (str[j] != ' ' && j != str.length -1) {
                j++;
            } else
            {
                if (j == (str.length -1))   {
                    j++;
                }
                rev(str,i,j);
                i=j+1;
                j=i;
            }
        }
        System.out.println(str);
    }
    
    0 讨论(0)
提交回复
热议问题