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

What is the use of pragma in embedded c?

592


Linked lists -- can you tell me how to check whether a linked list is circular?

646


What is typeof in c?

609


You are to write your own versions of strcpy() and strlen (). Call them mystrcpy() and mystrlen(). Write them first as code within main(), not as functions, then, convert them to functions. You will pass two arrays to the function in the case of mystrcpy(), the source and target array.

1783


Why is c called a mid-level programming language?

729






What is the condition that is applied with ?: Operator?

665


What is boolean in c?

613


I was asked to write a program in c which when executed displays how many no.of clients are connected to the server.

1907


What is volatile variable how do you declare it?

566


Is c weakly typed?

579


Q.1 write aprogram to stack using linklist o insert 40 items? Q.2 write a program to implement circular queue with help of linklist?

1600


Write a program for finding factorial of a number.

635


Why does everyone say not to use scanf? What should I use instead?

681


Do you know the use of fflush() function?

603


What are dangling pointers in c?

643