删除字符串中重复的字符

code

#include<stdio.h>
#include<string.h>

#define OUT_PUT_ARRAY_LEN  32

/*
* @brief Remove the duplicate letter from input string
*
* @param [in]str the input str pointer
* @param [out]pOutputStr the output str pointer
* @param [int|out]outLen the inputed output str length and the length of output str  
*
* @retval None
*
*/
void dedup(const char *str, char* pOutputStr, int *outLen)
{
    int i = 0;
    int j = 0;  
    int k = 0;
    if(NULL == str || NULL == pOutputStr  ){
        printf("The INPUT str || OUTPUT pOutputStr is : NULL !");
        return;
    }

    int stringLen = strlen(str);
    for(i = 0; i < stringLen && k < *outLen; i++){
        char testingChar = str[i];
        for(j = 0; j  < k; j++) {
            if(pOutputStr[j] == testingChar)
                break;
        }
        if( j == k) {
            pOutputStr[k] = testingChar;
            k++;
        }
    }
    pOutputStr[k] = '\0';
    *outLen = k;
}

int main(){
    int outLen = OUT_PUT_ARRAY_LEN;
    char outStr[OUT_PUT_ARRAY_LEN + 1];
    char *inputStr = "aabbbbcccccdddeeefffffaaaa";
    printf("The input string:%s\n", inputStr);
    dedup(inputStr, outStr, &outLen);
    printf("The output string:%s, the output string Len: %d\n", outStr, outLen);
                                                                                                           30,5          96%
    dedup(inputStr, outStr, &outLen);
    printf("The output string:%s, the output string Len: %d\n", outStr, outLen);
}
~  

Makefile :

CC=/opt/codesourcery/arm/bin/arm-linux-gcc 
DEST_OBJ := dedup 

CFLAGS += -static 

OBJ_DIR := .
SRCS    := dedup.c

OBJS    := $(patsubst %.c, $(OBJ)%.o, $(filter %.c, $(SRCS)))

all release debug: $(DEST_OBJ) 

$(DEST_OBJ) : $(OBJS)
        $(CC) $(CFLAGS) $(OBJS) -o $(DEST_OBJ) 


clean cleanall:
        rm  -fr $(DEST_OBJ) $(OBJS)

Push Shell :

chmod +x dedup;
adb connect 10.86.60.31;
adb -s  10.86.60.31:5555 remount ;
adb -s  10.86.60.31:5555 push  dedup /system/bin;
adb -s  10.86.60.31:5555 shell sync;

Test

这里写图片描述

其他方法

int GetResult(const char *input, char *output)
{
    if(input==NULL || output == NULL)
        return -1;
    vector<char> char_vec;
    vector<char>::iterator it;
    while(*input!='\0')
    {
        for(it=char_vec.begin();it!=char_vec.end();++it)
        {
            if(*it == *input)
            {
                break;
            }
        }
        if(it == char_vec.end())
        {
            char_vec.push_back(*input);
            *output++=*input;
        }
        ++input;
    }
    *output='\0';
    return 0;
}


int GetResult2(const char *input, char *output)
{
    if(input == 0 || output == 0)
        return -1;
    int i = 0;
    bool ch[129];
    for(i = 0; i < 129;i++)
        ch[i] = false;
    for(i = 0; *input != '\0' ; input++){
        if(!ch[*input]){
            ch[*input] = true;
            *output++ = *input;
        }
    }
    *output = '\0';
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值