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 meant by errors and debugging?
Ow can I insert or delete a line (or record) in the middle of a file?
write a program to generate address labels using structures?
main(){char *str;scanf("%s",str);printf("%s",str); }The error in the above program is: a) Variable 'str' is not initialised b) Format control for a string is not %s c) Parameter to scanf is passed by value. It should be an address d) none
What is malloc calloc and realloc in c?
What is the method to save data in stack data structure type?
What are structural members?
any "C" function by default returns an a) int value b) float value c) char value d) a & b
Why is struct padding needed?
what are the facialities provided by you after the selection of the student.
Write a program to print all permutations of a given string.
What is an lvalue?
What are the 4 types of unions?
what is event driven software and what is procedural driven software?
What is the process of writing the null pointer?