Home  >  Article  >  Backend Development  >  How to delete the same characters in a string in C language

How to delete the same characters in a string in C language

藏色散人
藏色散人Original
2020-02-07 11:34:377663browse

How to delete the same characters in a string in C language

c语言怎么删除字符串中相同字符?

c语言去除字符串中的重复字符

比如输入12eerer,输出12er

#include<stdio.h>
#include<string.h>
 
#define     MAX   100
 
 
int main()
{
    char str[MAX];
    char c;
    int strlen = 0;
    int i,j,k;
 
    scanf("%s",&str);//连续输入字符串
 
    for(i = 0;str[i] != &#39;\0&#39;;i++);
 
    strlen = i;
 
    for(i = 0;i <= strlen - 1;i ++)
    {
        for(j = i + 1;j <= strlen - 1;j ++)
        {
            if(str[i] == str[j])
            {
              
                 for(k = j;k <= strlen - 1;k ++)
                 {
                    str[k]=str[k + 1];
                 }
                 strlen --;
                 j --;//避免出现多个相同字符连在一起时出错的问题
                
            }
            
        }
        
    }
 
    str[strlen] = &#39;\0&#39;;
    
    printf("%s",&str);
    return 0;
}

 

推荐学习:c语言视频教程

The above is the detailed content of How to delete the same characters in a string in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn