求得所有包含在串s中而不包含在串t中的字符

 
  
1 using System;
2   using System.Collections.Generic;
3   using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 namespace DataStructure
8 {
9 class Client
10 {
11 // 算法要求:求得所有包含在串s中而不包含在串t中的字符(s中重复的字符只选一个)
12 // 构成的新串r,以及r中每个字符在s中第一次出现的位置
13
14 /// <summary>
15 /// 主方法
16 /// </summary>
17 public static void Main()
18 {
19 // 字符串S
20 string str_S = " 12345678abcdefgh345aefopqwer " ;
21 // 字符串T
22 string str_T = " 78aber " ;
23 // 字符List,存放新串r中的字符
24 List < char > resultList = new List < char > ();
25 // 存放r中字符第一次在s中出现的位置
26 List < int > posList = new List < int > ();
27 bool isExist;
28
29 // 将两个字符串转到字符数组中存放
30 char [] arr_S = str_S.ToCharArray();
31 char [] arr_T = str_T.ToCharArray();
32
33 for ( int i = 0 ; i < arr_S.Length; i ++ )
34 {
35 isExist = false ;
36 for ( int j = 0 ; j < arr_T.Length; j ++ )
37 {
38 if (arr_S[i] == arr_T[j])
39 {
40 isExist = true ;
41 // 在t中存在,则结束本趟循环
42 continue ;
43 }
44 }
45
46 // 保存符合条件的字符
47 if ( ! isExist)
48 {
49 resultList.Add(arr_S[i]);
50 posList.Add(i);
51 }
52 }
53
54 // 输出显示
55 for ( int k = 0 ; k < posList.Count; k ++ )
56 {
57 Console.WriteLine( " 字符: " + resultList[k] + " 在s中的位置: " + posList[k]);
58 }
59 Console.Read();
60 }
61 }
62 }

下面是测试结果(控制台程序):

http://images.cnblogs.com/cnblogs_com/falconshh/301631/r_Snap3.jpg

转载于:https://www.cnblogs.com/falconshh/archive/2011/05/26/2058028.html

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值