首页 > 试题广场 >

通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字

[问答题]
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
#include<iostream>
#include<cmath>
using namespace std;

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{
int hashtable[26]={0};
int j=0;
for(int i=0;i<lInputLen;i++)
{
if(hashtable[pInputStr[i]-'a']==0)
{
pOutputStr[j++]=pInputStr[i];
hashtable[pInputStr[i]-'a']=1;
}
}
}
void main( void )
{ 
char pInputStr[]="abacacde";  //"abcde"
char *pOutputStr;
stringFilter(pInputStr,sizeof(pInputStr),pOutputStr);
cout<<pOutputStr;
}

发表于 2015-07-18 20:48:31 回复(0)
void deleteN(char* pInput,int n){
    int len=strlen(pInput);
    for(int i=n;i<len;i++){
        pInput[i]=pInput[i+1];
    }
}
void stringFilterr(const char *pInputStr, long lInputLen, char *pOutputStr){
    int i=0;
    int j=0;
    pOutputStr=pInputStr;
    for(i=1;i<strlen(pOutputStr);i++){
        if(j=0;j<i;j++){
            if(pOutputStr[i]==pOutputStr[j]){
                deleteN(pOutputStr,i--);
            }
        }
    }
    for(i=0;i<strlen(pOutputStr);i++){
        printf("%c",pOutputStr[i]);
    }
    printf("\n");
}

发表于 2015-06-24 11:04:36 回复(0)
#include <iostream>
#include <cassert>

using namespace std;

bool g_flag[26];
void stringFilter( const char *pInputStr, long lInputLen, char *pOutputStr )
{
    assert( pInputStr != NULL );
    int i = 0;
    if ( pInputStr == NULL || lInputLen <= 1 )
    {
        return;
    }
    const char *p = pInputStr;
    while ( *p != '\0' )
    {
    if ( g_flag[(*p - 'a')] )
    {
        p++;
    }else{
        pOutputStr[i++]		= *p;
        g_flag[*p - 'a']	= 1;
        p++;
         }
    }
    pOutputStr[i] = '\0';
}
int main()
{
    memset( g_flag, 0, sizeof(g_flag) );
    char	input[] = "abacacde";
    char	*output = new char[strlen( input ) + 1];
    stringFilter( input, strlen( input ), output );
    cout << output << endl;
    delete output;
    return(0);
}


发表于 2014-11-15 16:34:22 回复(0)