In: Computer Science
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
input code:
output:
code:
#include <iostream>
#include<vector>
using namespace std;
int main()
{
/*vector for store characters*/
vector<char> s;
char ch='a';
/*take characters one by one*/
cout<<"Enter a sequence of characters:\n";
cin.get(ch);
while(ch!=' ')
{
/*flag */
int flag=1;
/*check already entered or not*/
for(int i=0;i<s.size();i++)
{
/*if found than make flag=0*/
if(s[i]==ch)
{
flag=0;
break;
}
}
/*if flag=1 than add into vector*/
if(flag==1)
{
s.push_back(ch);
}
/*take characters again*/
cin.get(ch);
}
/*output string*/
string output="";
/*store into string from vector*/
for(int i=0;i<s.size();i++)
{
output+=s[i];
}
/*print a string*/
cout<<"Distinct characters are: "<<output;
return 0;
}