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
When do we get logical errors?
Explain what are the different data types in c?
Is array a primitive data type in c?
Explain how do you list a file’s date and time?
What is wrong in this statement?
When is a void pointer used?
What are the advantages and disadvantages of a heap?
By using C language input a date into it and if it is right?
With the help of using classes, write a program to add two numbers.
.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }
How macro execution is faster than function ?
How many main () function we can have in a project?
Explain how can I pad a string to a known length?
Is it cc or c in a letter?
How to create struct variables?