write a c program to remove all the duplicate characters in a
string and replace with single character?
ex:-input- AAABBBCCC
output- ABC
Answer Posted / satya
//using the std::string class from namespace std.
#include<iostream>
using namespace std;
int main()
{
string myStr;
cout<<"enter new string.";
getline(cin,myStr);
cout<<"entered value is "<<myStr;
char ch;
bool m=false;
string newStr;
newStr.resize(1);
int k=0;
for(int i=0;i<myStr.length();i++)
{
ch=myStr[i];
for(int j=0;j<k+1;j++)
{
if(ch!=newStr[j]) m=false;
else { m=true; break;}
}
if(m==false)
{
newStr.resize(newStr.size()+1);
newStr[++k]=ch;
}
}
cout<<"\nAfter removing duplicate letters, string is "<<newStr;
}
| Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
Why do some versions of toupper act strangely if given an upper-case letter?
What are the 4 data types?
what are the 10 different models of writing an addition program in C language?
There seem to be a few missing operators ..
Why is a semicolon (;) put at the end of every program statement?
Difference between Shallow copy and Deep copy?
What is the purpose of type declarations?
What does s c mean in text?
Write a program to implement a round robin scheduler and calculate the average waiting time.Arrival time, burst time, time quantum, and no. of processes should be the inputs.
Why c is called free form language?
What happens if a header file is included twice?
Find duplicates in a file containing 6 digit number (like uid) in O (n) time.
What are the two forms of #include directive?
int i=10; printf("%d %d %d", i, i=20, i);
Explain the difference between #include "..." And #include <...> In c?