删除所有字符串复制charcters - Ç [英] Delete all duplicated charcters from strings - c

查看:110
本文介绍了删除所有字符串复制charcters - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从给定的字符串中删除所有重复的字符。
例如,asdasd=房间隔缺损或abbgga=ABG。

当我编译并在CMD开始运行它,我收到停止工作的消息。

这是我的code:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;无效delMul(字符STR []){
    INT I,J = 0;
    的int [26];
    字符K表;
    对于(i = 0; I< 26;我++)
    {
        S [I] = 0;
    }
    对于(i = 0; I<的strlen(STR);我++)
    {
        K = STR [I] - 'A';
        如果(S [k]的== 0){
            S [k]的++;
            STR [J ++] = STR [I]
        }
    }
    海峡[J] ='\\ 0';
}诠释主(){
    char *之海峡=asdasd;
    delMul(STR);
    看跌期权(STR);
}


解决方案

在您的code, STR 指向的字符串的,这可能会不被修改。尝试使用由字符串初始化相同的数组。

I am trying to delete all duplicate characters from a given string. For example "asdasd" = "asd" or "abbgga" = "abg".

When I compile and when cmd starts to run it I am getting "stopped working" message.

This is my code :

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

void delMul(char str[]){
    int i,j=0;
    int s[26];
    char k ;
    for ( i = 0; i < 26; i++)
    {
        s[i] = 0;
    }
    for (i = 0; i < strlen(str); i++)
    {
        k = str[i] - 'a'; 
        if(s[k] == 0) {
            s[k]++ ;
            str[j++] = str[i];
        }
    }
    str[j] = '\0';
}

int main(){
    char *str = "asdasd";
    delMul(str);
    puts(str);
}

解决方案

In your code, str points to a string literal, which might not be modified. Try using an array for the same initialized by the string.

这篇关于删除所有字符串复制charcters - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆