如何使用指针和数组在c中一对一地反转单词

问题描述

|| 嘿,是的,你猜对了作业。我正在尝试使用指针反向打印字符串。虽然只有话。因此,“ hello world”是“ olleh dlrow”。 我在下面的代码中所做的是将一个指针(pSentence,这是从主函数向下传递的多个单词组成的字符串数组)分配给一个临时指针,直到一个空格,然后向后递增并从该临时对象打印字符指向单词开头的指针,然后再次执行。我目前处于困境,我不知道如何标记单词的开头并仅增加到该单词的开头。我知道while(pt!= \'0 \')根本不是做到这一点的方法。提示说将单词存储到一个临时字符串(tmpStrg)中并使用pT指向它,所以也许我需要对tmpStrg做些事情?非常感谢您的任何帮助,并在此先感谢您!!
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--;
       }
    }
}
    

解决方法

void prtWords(char *pSentence)
{
    char tmpStrg[81],*pT=tmpStrg;
根据您的代码,我想在这里您将
tmpStrg[81]
用作一个单词的新字符串,然后以相反的顺序打印该单词。
    int length=0;
    while(*pSentence != \'\\0\')
    {
       while(*pSentence != \' \')
       {
这是您的溢出问题,您应该同时测试
\\0
(并将第二个
while
更改为
&&
,而第一个
while
保持不变)。不能保证句子以ѭ5结尾。
         *pT=*pSentence;
         pT++;
         pSentence++;
         length++;
       }
       pSentence++;
       while(length >= 0);
length
0
时,您不应做任何事情。更改为
>
       {
         printf(\"%c\",*pT);
您应该先进行
pT--
,因为最后
pT++
没有分配。
         pT--;
         length--;
       }
*pSentence == \' \'
时,在此打印a5ѭ。
    }
}
自己编写完整的代码,因为这是家庭作业。任何其他问题都欢迎。     ,@Duck的评论“将问题分解成碎片。”突出显示了最重要的设计/编程技术之一。 我不熟悉哪种单元测试框架最常用于测试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:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...