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


Please Help Members By Posting Answers For Below Questions

the process of defining something in terms of itself is called (or) in C it is possible for the functions to call themselves. A function called a) nested function b) void function c) recursive function d) indifinite function

996


What is the difference between a string and an array?

902


What does void main () mean?

947


What is 'bus error'?

846


Apart from dennis ritchie who the other person who contributed in design of c language.

1069


Why clrscr is used in c?

788


What are terms in math?

782


How can I pad a string to a known length?

781


Explain how can I make sure that my program is the only one accessing a file?

856


Where we use clrscr in c?

886


What is the explanation for prototype function in c?

735


Can we increase size of array in c?

705


what value is returned to operating system after program execution?

1804


Write a program on swapping (100, 50)

854


Explain built-in function?

804