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

藏色散人
Release: 2020-02-07 11:34:37
Original
7608 people have browsed it

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;
}
Copy after login

 

推荐学习: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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!