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

后端 未结 12 1486
伪装坚强ぢ
伪装坚强ぢ 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 04:51

    using stack 
    
    #include   
    #include 
    #include 
    
    int main()
    { 
    
        std::stack st;
        char *words= "this is a test";
        char * temp =   (char *)calloc(1, sizeof(*temp));
        int size1= strlen(words);
        int k2=0;
        int k3=0;
        for(int i=0;i<=size1;i++)
        {
           temp[k2] = words[i];
           k2++;
            if(words[i] == ' ')     
            {  
                k3++;
                if(k3==1)
                    temp[k2-1]='\0';
    
                temp[k2]='\0';
                st.push(temp);
                k2=0;           
            }
            if(words[i] == '\0')
            {
                temp[k2]='\0';
                st.push(temp);
                k2=0;
                break;
            }               
        }
    
      while (!st.empty())
      {
           printf("%s",st.top().c_str());
            st.pop();
      }
    

提交回复
热议问题