c++ - Replacing duplicates in a string -


i trying write code replace duplicates in string '*' . code have written is-

#include<iostream> #include<math.h> #include<string> using namespace std;   int main() {   string ch;   cin>>ch;   for(int i=0;i<ch.length()&& (ch[i]!='*');i++)   {       for(int j=i+1;j<ch.length()&&ch[j]!='*';j++)          {         if(ch[i]==ch[j])ch[j]='*';       }   }     cout<<ch; } 

the above code gives unexpected results inputs. e.g. if input "adad" answer "ad*d", whereas output "adda", , "aadd" matches expected output. can tell me i'm doing wrong ?

like piotr s. said in comments, need keep processing string, if current character *. here take, should work:

#include<iostream> #include<math.h> #include<string> using namespace std;   int main() {     string ch;     cin >> ch;     for(int = 0; < ch.length(); ++i)     {         if(ch[i] == '*') continue; // skip entry         for(int j = + 1; j < ch.length(); ++j)            {             if(ch[i] == ch[j]) ch[j]='*';         }     }     cout<<ch; } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -