-
题目描述:
-
对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12
输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。
-
输入:
-
输入包括一个由字母和数字组成的字符串,其长度不超过100。
-
输出:
-
可能有多组测试数据,对于每组数据,
按照样例输出的格式将字符出现的位置标出。
-
样例输入:
-
abcaaAB12ab12
-
样例输出:
-
a:0,a:3,a:4,a:9 b:1,b:10 1:7,1:11 2:8,2:12
-
提示:
-
1、下标从0开始。
2、相同的字母在一行表示出其出现过的位置。
-
代码:
-
还可以用其他方法,但是回顾一下链表吧!
# include <stdio.h> # include <string.h> # include <stdlib.h> typedef struct shead { char c; int pos; struct shead* next; }shead; shead sh[100]; int search (int n,char c) { int rs=-1,i; for (i=0;i<n;i++) { if (sh[i].c==c) { rs=i; break; } } return rs; } int main () { char s[102]; int i,n,pos; shead* p; shead* q; while (scanf ("%s",s)!=EOF) { n=0; for (i=0;i<strlen(s);i++) { pos=search(n,s[i]); if (pos!=-1) { p=(shead*)malloc(sizeof(shead)); p->c=s[i]; p->pos=i; p->next=NULL; q=sh+pos; while (q->next!=NULL) q=q->next; q->next=p; } else { sh[n].c=s[i]; sh[n].pos=i; sh[n].next=NULL; n++; } } for (i=0;i<n;i++) { if (sh[i].next!=NULL) { p=sh+i; while(p!=NULL) { printf ("%c:%d",p->c,p->pos); if (p->next!=NULL) printf (","); p=p->next; } printf ("\n"); } } } return 0; }
本文介绍了一个C语言程序,用于找出给定字符串中重复出现的字符及其位置。通过使用链表结构,程序能有效地记录每个字符的所有出现位置,并按指定格式输出结果。
1739

被折叠的 条评论
为什么被折叠?



