• 字符串过滤


    一、题目描述(60分):
    通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
    比如字符串“abacacde”过滤结果为“abcde”。

    要求实现函数:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);

    【输入】 pInputStr:  输入字符串
                lInputLen:  输入字符串长度         
    【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 

    【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出

    示例 
    输入:“deefd”        输出:“def”
    输入:“afafafaf”     输出:“af”
    输入:“pppppppp”     输出:“p”

    main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出
    当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<iostream>
     4 #include<string.h>
     5 using namespace std;
     6 void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
     7 {
     8     int b[26],i,j;
     9     j = 0;
    10     for(i=0; i < 26; i++)
    11         b[i] = 0;
    12     for(i=0; i < lInputLen; i++)
    13         if(!b[pInputStr[i] - 'a'])
    14             {
    15                 pOutputStr[j++] = pInputStr[i];
    16                 b[pInputStr[i] - 'a']++;
    17             }
    18           pOutputStr[j] = '';
    19     printf("%s
    ", pOutputStr);
    20 
    21 }
    22 
    23 int main()
    24 {
    25     char s[40],s1[40];
    26     char *s2 = (char*)malloc(sizeof(char) * 40);
    27     while(scanf("%s",s))
    28     {
    29         stringFilter(s,strlen(s),s1);
    30         
    31     }
    32 
    33 }

  • 相关阅读:
    继承与多态
    本周总结
    总结
    周总结
    周总结
    第三周总结
    .......
    .....
    ....
    ....
  • 原文地址:https://www.cnblogs.com/george-cw/p/3933365.html
Copyright © 2020-2023  润新知