Friday 24 January 2014

Program to determine if a string has unique characters

This is a simple program to check if all the characters in a string are unique or not if no additional data structure is provided.
  1 #include<iostream> 
  2 using namespace std;
  3 int main() 
  4 { 
  5     int i,l,j; 
  6     bool f=true; 
  7     char a[50];
  8     cout<<"Enter: "; 
  9     gets(a); 
 10     l=strlen(a); 
 11     for(i=0;i<l;i++) 
 12     { 
 13         for(j=l-1;i<j;j--) 
 14         if(a[i]==a[j]) 
 15         { 
 16             f=false; 
 17             break; 
 18         } 
 19     } 
 20     if(f) 
 21         cout<<"All unique"; 
 22     else 
 23         cout<<"Not unique"; 
 24     return 0;
 25 } 
 26 

No comments:

Post a Comment