Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
How to Find Repeated Characters in a string using C Program.

Can Any one help me?

regards
Sarath

[edit]Tags only - OriginalGriff[/edit]
Posted
Updated 30-Sep-10 23:30pm
v2
Comments
Sandeep Mewara 1-Oct-10 11:44am    
No effort

Loop through each character in the string, checking it against the last one.
Which bit of this are you having problems with?
 
Share this answer
 
Create an array of bits, one per possible character. Read each character in turn and set the corresponding bit in the arry. Bail out if you try and set a bit that's already set.

Cheers,

Ash

PS: Unless you mean two characters the same next to each other in the string, in which case Griff's answer is the way to go.
 
Share this answer
 
Comments
OriginalGriff 1-Oct-10 9:35am    
5! I hadn't thought of that interpretation - good thought. I would probably just sort the characters then treat as for my interpretation though.
#include<stdio.h>
struct verify
{
char s;
int flag;

}pt[100];
struct count
{
char q;
int count;
}st[100];

main()
{
char p[100];// = "LIFETREE";
int i, j,k;
printf ("enter the string \n");
scanf("%s", p);
for(i;p[i] != '\0'; i ++)
{
pt[i].s=p[i];
pt[i].flag=0;
}
p[++i] ='\0';
for(i=0;p[i] != '\0' ;i++)
{

st[i].q=p[i];
if(pt[i].flag == 0)
{

st[i].count=1;
pt[i].flag = 1;
for(j = i+ 1;p[j] != '\0'; j++)
{
if(p[i] == p[j])
{
st[i].count += 1;
pt[j].flag =1;
}
}
}
}
for(k = 0;k < i;k ++)
{
if(st[k].count > 0)
printf ("%c %d\n", st[k].q,st[k].count);
}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900