how to perform reversing a sentence Word by Word in C?

后端 未结 12 1330
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 04:49
#include 

int main(void)
{
  int i,j;
  int wordstart = -1;
  int wordend = -1;
  char words[]= \"this is a test\";
  char temp;

  // Reverse each word
         


        
相关标签:
12条回答
  • 2021-01-25 05:06
     #include <iostream>
     #include <string>   
      using namespace std;
      char* stringrev(char s[], int len)
      {
        char *s1 = (char*)malloc(len+1);
        int i=0;
        while (len>0)
        {
          s1[i++] = s[--len];
        }
       s1[i++] = '\0';
        return s1;   
      }
    
       void sentrev(char s[], int len)
     {
        int i=0; int j=0;
         char *r = (char*)malloc(len+1);
         while(1)
         {
         if(s[j] == ' ' || s[j] == '\0')
         {
           r = stringrev(s+i, j-i);
           i = j+1;
           cout<<r<<" ";
         }
        if (s[j] == '\0')
        break;
        j++;
       }
    
     }
    
    
    int main()
    {
    char *s = "this is a test";
    char *r = NULL;
    int len = strlen(s);
    cout<<len<<endl;
    r = stringrev(s, len);
    cout<<r<<endl;
    sentrev(r, len);
    return 0;
    }
    

    The above code snap reverse the sentence, using char *r and printing cout<

    0 讨论(0)
  • 2021-01-25 05:10
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int main()
    {
    char st[50], rst[50];
    printf("Enter the sentence...\n");
    gets(st);
    int len=strlen(st), p;
    int j=-1,k;
    p=len;
    for(int i=(len-1); i>=0; i--)
    {
        //searching for space or beginning
        if(st[i]==' ')
        {
            //reversing and storing each word except the first word
            for(k=i+1;k<p;k++)
            {
                //printf("%c",st[k]);
                rst[++j]=st[k];
            }
            j++;
            rst[j]=' ';
            printf("\n");
            p=i;
        }
        else if(i==0)
        {
            //for first word
            for(k=i;k<p;k++)
            {
                //printf("%c",st[k]);
                rst[++j]=st[k];
            }
        }
    
    }
    printf("Now reversing the sentence...\n");
    puts(rst);
    return 0;
    }
    
    0 讨论(0)
  • 2021-01-25 05:11

    You can create a double linked list as a base data structure. Then, iterate through the words and insert them in the list as you find them.

    When you reach the end of the sentence, simply traverse the list backwards and print the words as you go through them

    0 讨论(0)
  • 2021-01-25 05:12

    Start tokenizing the line from the last character and continue to the first character. Keep one pointer anchored at the base of the current word, and another pointed which will decrease while a word start is not found. When you find a word start while scanning like this, print from the word start pointer to the word end anchor. Update the word end anchor to the previous character of the current word start char.

    You might want to skip the blankspace characters while scanning.

    UPDATE

    This is a quick implementation:

    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAX_BUF 256
    
    void show_string (char *str, int i, int n)
    {
      while (i <= n)
      {
        printf ("%c", str[i]);
        i++;
      }
    }
    
    int main (void)
    {
      char str[MAX_BUF];
      int end_anchor, start_ptr;
      int state;
    
      printf ("\nEnter a string: ");
      scanf (" %[^\n]", str);
    
      start_ptr = strlen (str) - 1;
    
      end_anchor = start_ptr;
      state = 0;
      while (start_ptr >= -1)
      {
        switch (state)
        {
          case 0:
                 if ((!isspace (str[start_ptr]) && (start_ptr >= 0)))
                 {
                   start_ptr--;
                 }
                 else
                 {
                   state = 1;
                 }
                 break;
    
          case 1:
                 show_string (str, start_ptr + 1, end_anchor);
                 state = 2;
                 start_ptr--;
                 printf (" ");
                 break;
    
          case 2:
                 if (!isspace (str[start_ptr]))
                 {
                   state = 0;
                   end_anchor = start_ptr;
                 }
                 else
                 {
                   start_ptr--;
                 }
                 break;
        }
      }
    
    
      printf ("\n");
      return 0;
    }
    

    The end_anchor points to each end word, and the start_ptr finds the start of the word of which the end is held by end_anchor. When we find a word start (by blankspace characters or start_ptr = -1), we print all the characters from start_ptr + 1 to end_anchor. The + 1 is because of the implementation: start_ptr points to the blankspace character, and the print routine will print all the characters from i to n. Once we have detected one blank space we print it and we skip adjacent blankspaces (in case 2) and preserve only one which is manually printed. Once a non blankspace is detected, we have got another word end, for which we set the end_anchor to this index in the case 2, and set state = 0 , so that we can search for the word start again.

    0 讨论(0)
  • 2021-01-25 05:12

    Use a main for loop to traverse till the end of the sentence: Copy the letters in a string until you find a space. now call add@beginning function and in that function add the string each time you pass a string to the linked list. print the contents of the linked list with a space inbetween to get the expected output

    0 讨论(0)
  • 2021-01-25 05:14

    My code,just traverse from the last and if you find a space print the characters before it,now change the end to space-1;This will print till the second word,finally just print the first word using a single for loop.Comment for alter approach.

    Program:

    #include<stdio.h>
    int main()
    {
     char str[200];
    int i,j,k;
    scanf("%[^\n]s",&str);
    for(i=0;str[i]!='\0';i++);
    i=i-1;
    for(j=i;j>=0;j--)
    {
        if((str[j])==' ')
        {
            for(k=j+1;k<=i;k++)
            {
                printf("%c",str[k]);
            }
            i=j-1;
            printf(" ");
        }
    
    }
    for(k=0;k<=i;k++)
    {
        printf("%c",str[k]);
    }
    }
    
    0 讨论(0)
提交回复
热议问题