Write a program to print unique characters in string


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Write a program to print unique characters in string
// Ex: AimForJob -> AimForJb; success -> suce
// Author: AimForJob.com
 
#include<string.h>
#include<stdio.h>
main(){
    char str[100],result[100]={0};
    puts("Enter any string");
    gets(str);                        // string input from user
    int len,i,j,count,k=0;
    len=strlen(str);
    for(i=0;i<len;i++){
        count=0;
        for(j=0;j<i;j++){
            if(str[i]==result[j]){    // check if char is unique
                count++;
            }
        }
        if(count==0){
            result[k++]=str[i];
        }
    }
    printf("Output string with only unique characters:");
    puts(result);
    getch();
}

No comments:

Post a Comment