2018-10-21  本文已影响0人  ahuustcly

1.给定两个字符串,求出它们之间最长公共子串

2.输入一个英文句子,翻转句子中单词的顺序,但是单词中的字符顺序不变

void reverse(char *pStart, char *pEnd)
{
    char temp;
    if (NULL == pStart || NULL == pEnd)
    {
        return;
    }
    while (pStart < pEnd)
    {
        temp = *pStart;
        *pStart = *pEnd;
        *pEnd = temp;
        
        pStart++;
        pEnd--;
    }
}

/*
输入一个英文句子,翻转句子中单词的顺序,但是单词内的字符顺序不变
用例:
①句子中有多个单词、一个单词
②NULL、字符串指向的内容为空、字符串只有空格
*/
char *reverseSentence(char *pSentence)
{
    if (NULL == pSentence || strlen(pSentence) <= 1)
    {
        return pSentence;
    }

    char *pStart = pSentence;
    char *pEnd = pSentence + strlen(pSentence) - 1;
    reverse(pStart, pEnd);

    pStart = pEnd = pSentence;
    while (*pStart != '\0')
    {
        if ((*pEnd <= 'z' && *pEnd >= 'a') ||
            (*pEnd <= 'Z' && *pEnd >= 'A'))//正常单词字符
        {
            pEnd++;
        }
        else if (pStart != pEnd)
        {
            reverse(pStart, pEnd-1);
            if (*pEnd != '\0')
                pStart = ++pEnd;
            else
                break;
        }
        else
        {
            pStart = ++pEnd;
        }
    }
    return pSentence;
}

上一篇 下一篇

猜你喜欢

热点阅读