write a c program to remove all the duplicate characters in a
string and replace with single character?
ex:-input- AAABBBCCC
output- ABC

Answers were Sorted based on User's Feedback



write a c program to remove all the duplicate characters in a string and replace with single char..

Answer / vikas

// removal of duplicate character form a given string
#include <string.h>
#include <stdio.h>

int main()
{
char os[30];
char ds[20];
int i=0,j=0, c;

printf("Enter string\n");
while ((c = getchar()) != '\n')
os[i++] = c;
os[i] = '\0';

ds[0] =os[0];
ds[1] = '\0';
i = 1;
while ( os[i] != '\0'){
j = 0;
while (ds[j] != '\0') {
if (ds[j] == os[i])
break;
else
j++;
}
if (ds[j] == '\0') {
ds[j] = os[i];
ds[++j] = '\0';
}
i++;
}
printf("Original string = %s\n", os);
printf("modified string = %s\n", ds);
return 0;
}

Is This Answer Correct ?    13 Yes 4 No

write a c program to remove all the duplicate characters in a string and replace with single char..

Answer / 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

More C Interview Questions

What is the difference between malloc() and calloc()?

0 Answers  


which one is better structure or union?(other than the space occupied )

2 Answers  


how many errors in c explain deply

0 Answers  


how to get starting address of a running C program

3 Answers  


Write a program for the following series? 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 1234567890987654321 123456789010987654321 12345678901210987654321 1234567890123210987654321 .........1234321............ ..........123454321............ ..........12345654321............ 7 8 9 0 1 Pls............?

5 Answers  






What is the meaning of && in c?

0 Answers  


Explain how can I convert a number to a string?

0 Answers  


What is variable and explain rules to declare variable in c?

0 Answers  


will the program compile? int i; scanf(ā€œ%dā€,i); printf(ā€œ%dā€,i);

3 Answers  


Explain how do you override a defined macro?

0 Answers  


What does *p++ do? What does it point to?

0 Answers  


What is a buffer in c?

0 Answers  


Categories