c++如何实现字符消元

Description

小黑同学最近游戏玩多了,变成黑眼圈,输入代码或字符时,经常会因为眼花手抖重复输入多余字符。比如:经常把printf输成ppriintfff;输入work时,经常会输成wworkkkk。
请编写程序,帮忙小黑同学把输入字符串中连续相同的多余字符消去(保留连续相同字符中的1个)。

Input

一行字符,不超过80个。

Output

消去连续相同的多余字符后的字符串。

Sample Input

样例1:
Connectting the WWworld!!!!

样例2:
It'ss haaarrd woork tto leearn C lannnguagggee
Sample Output

样例1:
Conecting the Wworld!

样例2:
It’s hard work to learn C language


#include <stdio.h>

int main()
{
    char str[80], *p, *q;

    gets(str);
    p = q = str;

    while (*p)
    {
        while (((*q) == (*p)) && ((*p) != 0))
            p++;
        if (*p)
        {
            q++;
            (*q) = (*p);
            p++;
        }
    }
    q++;
    (*q) = 0;
    printf("%s\n", str);
    return 0;
}