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
What is the right type to use for boolean values in c? Is there a standard type? Should I use #defines or enums for the true and false values?
Why is it usually a bad idea to use gets()? Suggest a workaround.
Do character constants represent numerical values?
I came across some code that puts a (void) cast before each call to printf. Why?
What is memcpy() function?
How do you declare a variable that will hold string values?
Is it cc or c in a letter?
What is scanf () in c?
Explain what is the use of a semicolon (;) at the end of every program statement?
How to create struct variables?
What is %d called in c?
Can you apply link and association interchangeably?
How can I trap or ignore keyboard interrupts like control-c?
What is a good data structure to use for storing lines of text?
program to find error in linklist.(i.e find whether any node point wrongly to previous nodes instead of next node)