【C语言】 删除一个字符串中重复的字符

#include<stdio.h>
/*使用n=strlen(s)时加这个#include<string.h>*/
int main(void)
{
    char s[100];/*定义变量*/
    int i, j, n, k;
    printf("请输入一串有重复字符的字符串:\n");
    gets_s(s);/*输入字符串*/
    for (n = 0; s[n] != '\0'; n++);/*用于得到字符串长度,也可以使用n=strlen(s);*/
    for (i = 0; i < n - 1; i++)
    {
        for (j = k = i + 1; j < n - 1; j++)
            if (s[j] != s[i])
                s[k++] = s[j];
        s[k] = '\0';
    }
    printf("去掉重复字符后结果为:\n");
    puts(s);
    /*也可以这样写,printf("去掉重复字符后结果为:%s\n", s);*/
}  

猜你喜欢

转载自www.cnblogs.com/HGNET/p/11966034.html