How to undo words one by one in c using pointers and arrays

Hey yes, you guessed it was homework. I am trying to print a string in reverse order with pointers. Only words. So hello world is olleh dlrow. What I do in the code below assigns one pointer (pSentence, which is a string array of several words passed from the main function) to a temporary pointer to a space, then increments it back and prints the character from the temporary pointer to the beginning of the word, then do it again. Iโ€™m stuck right now, I donโ€™t know how to mark the beginning of a word and grow only to this. I know that while (pt! = '0') is not a way to do this at all. The tooltip says to save the word in a temporary line (tmpStrg) and use pT to point to it, so maybedo i need to do something using tmpStrg? Any help is greatly appreciated and thank you in advance.

void prtWords(char *pSentence)
{
    char tmpStrg[81], *pT=tmpStrg;
    int length=0;

    while(*pSentence != '\0')
    {
       while(*pSentence != ' ' && *pSentence != '/0')
       {
         *pT=*pSentence;
         pT++;
         pSentence++;
         length++;
       }
       pSentence++;

       while(length >= 0);
       {
         printf("%c", *pT);
         pT--;
         length--;
       }
    }
}
+3
2
void prtWords(char *pSentence)
{
    char tmpStrg[81], *pT=tmpStrg;

, , tmpStrg[81] , .

    int length=0;
    while(*pSentence != '\0')
    {
       while(*pSentence != ' ')
       {

, \0 ( while && while ). , .

         *pT=*pSentence;
         pT++;
         pSentence++;
         length++;
       }
       pSentence++;
       while(length >= 0);

length - 0, . >.

       {
         printf("%c", *pT);

pT--, pT++ .

         pT--;
         length--;
       }

, *pSentence == ' '.

    }
}

, . .

+1

@Duck comment " ". , /.

, C, - .

, , :

void ReverseInPlace(char * pstart, char * pend)
{
    char tmp;

    while (pend > pstart)
    {
        tmp = *pstart;
        *pstart++ = *pend;
        *pend-- = tmp;
    }
}

void ReverseWordsInPlace(char *pSentence) 
{
    char * pstart;
    char * pend; 
    pstart = pSentence;

    while (*pstart != '\0')
    {
        // skip any (multiple) starting spaces
        while (*pstart == ' ')
        {
            pstart++;
        }

        pend = pstart;

        // find end of word (terminated by a space or end of string)
        while (*pend != ' ' && *pend != '\0')
        {
            pend++;
        }

        // check if anything left to do
        if (pstart >= pend - 1)
            return;

        ReverseInPlace(pstart, pend - 1);

        pstart = pend;
    }
}

int main(int argc, char * argv[])
{

char string1[] = "The quick brown fox jumped over the lazy dog";
char string2[] = "_";
char string3[] = " ";
char string4[] = " another";
char string5[] = "hello ";  
char string6[] = "";
char string7[] = "  ab";

 ReverseWordsInPlace(string1);
 ReverseWordsInPlace(string2);
 ReverseWordsInPlace(string3);
 ReverseWordsInPlace(string4);
 ReverseWordsInPlace(string5);
 ReverseWordsInPlace(string6);
 ReverseWordsInPlace(string7);

printf("%s\n", string1);
printf("%s\n", string2);
printf("%s\n", string3);
printf("%s\n", string4);
printf("%s\n", string5);    
printf("%s\n", string6);
printf("%s\n", string7);

return 0; 
}
+1

All Articles