[C] PTA remove duplicate characters

This problem requires programming, given the repeated character string is removed, according to the ASCII character sequence in ascending order are output.

Input formats:

Is input to a non-empty string enter the end (less than 80 characters).

Output formats:

To re-output the result sorted string.

Sample input:

ad2f3adjfeainzzzv

Sample output:

23adefijnvz

Ideas:

 First string of characters in the ASCII code sequence from small to large order, and then the sorted array remove duplicate entries.

#include<stdio.h>
#include<string.h>

char* sortArray(char *str, int n);
char* deleteDuplicate(char *str, int n);

int main(){
    char str[80];
	gets(str);
    int n = strlen(str);
    sortArray(str,n);
    printf("%s",deleteDuplicate(str,n));
}
/**
*选择排序 
*/
char* sortArray(char *str, int n){
    for(int i=0; i<n-1; i++){
        char min=str[i];
        int minIdx=i;
        for(int j=i+1; j<n; j++){
            if(str[j]<min){
                min=str[j];
                minIdx=j;
            }
        }
        if(min<str[i]){
            char t=str[i];
            str[i]=min;
            str[minIdx]=t;
        }
    }
    return str;
}
/**
*对排序后的数组删除重复项 
*/ 
char* deleteDuplicate(char *str, int n){
    char* ret=str;
    ret[0]=str[0];
    int j=1;
    for(int i=1; i<n; i++){
        if(str[i]==str[i-1]){
            continue;
        }else{
            ret[j]=str[i];
            j++;
        }
    }
    ret[j]='\0';
    return ret;
}

 

Published 37 original articles · won praise 47 · Views 100,000 +

Guess you like

Origin blog.csdn.net/u013378642/article/details/104886020