如何在字符串中查找重复的字符? [英] How to Find repeated Charaters in a String?

查看:404
本文介绍了如何在字符串中查找重复的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何使用C程序查找字符串中的重复字符.

谁能帮我吗?

问候
萨拉斯

[edit]仅标记-OriginalGriff [/edit]

Hi,
How to Find Repeated Characters in a string using C Program.

Can Any one help me?

regards
Sarath

[edit]Tags only - OriginalGriff[/edit]

推荐答案

遍历字符串中的每个字符,并与最后一个字符进行比较.
您对此有什么疑问?
Loop through each character in the string, checking it against the last one.
Which bit of this are you having problems with?


创建一个位数组,每个可能的字符一个.依次读取每个字符并在arry中设置相应的位.如果您尝试设置已经设置的位置,请进行纾困.

干杯,



PS:除非您是指字符串中两个彼此相邻的字符,否则,格里夫(Griff)的答案就是解决方法.
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.


#include< stdio.h>
结构验证
{
char s;
int标志;

} pt [100];
结构计数
{
字符q;
整数计数;
} st [100];

main()
{
char p [100];//="LIFETREE";
int i,j,k;
printf(输入字符串\ 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);
}

}
#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);
}

}


这篇关于如何在字符串中查找重复的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆