从一个字符串中删除字符 [英] Remove a character from a string

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

问题描述

我想从一个字符串创建一个函数指定字符

我只用一半的code这样做,因为我当我试图取代删除与任何角色卡住了。我才意识到我不能把一无所有在一个数组的元素,所以我的计划刚刚毁了。

我的身影,我经历了整个字符串必须循环,当我找到角色,我想删除我必须将所有的都是坏字面前退一步元素将其删除。这是否正确?

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;无效德尔(字符字符串[],CHAR charToDel)
{    INT索引= 0;    而(字符串[指数]!='\\ 0')
    {
        在(string [指数] == charToDel){
            字符串[指数] =字符串[索引+ 1];
        }        指数++;
    }    的printf(%S,字符串);
}INT主要(无效)
{    字符字符串[] =Hello World的;    德尔(字符串,'L');    返回0;
}

我想使这个节目没有引用。只是朴素简单code。

我添加了另一个while循环,在循环中移动的每一个字符的离开,但它似乎并没有工作,因为输出只是普通的空白。

  INT指数= 0;
    而(字符串[指数]!='\\ 0')
       {
           在(string [指数] == charToDel)
           {
                而(字符串[指数]!='\\ 0')
                {
                    字符串[指数] =字符串[索引+ 1];
                }           }           指数++;
       }    的printf(%S,字符串);
}

Johathan莱弗勒的方法?

 字符newString [100];    INT索引= 0;
    INT I = 0;    而(字符串[指数]!='\\ 0')
       {
           在(string [指数]!= charToDel)
           {
                newString [I] =字符串[指数]                指数++;
                我++;           }
           我++;
           指数++;
       }    的printf(%S,newString);
}

这给了我很多的奇怪的字符...


解决方案

 字符常量*在=串;
字符* OUT =字符串;而(*中){
    如果(*在!= charToDel)
        *总分++ = *的;
    ++中;
}*总分='\\ 0';

或没有指针

 为size_t在= 0;
size_t型出= 0;而(字符串[中]){
    在(string [中]!= charToDel)
         字符串[OUT +] =字符串[中];
    ++中;
}字符串[出] ='\\ 0';

I am trying to create a function that removes a specified character from a string.

I am only halfway done with the code because I got stuck when I am trying to replace the character to delete with nothing. I just realized I can't put "nothing" in an element of an array so my plan just got ruined.

I figure that I have to loop through the whole string, and when I find the character I want to remove I have to remove it by moving all of the elements that are in front of the "bad" character one step back. Is that correct?

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

void del(char string[], char charToDel)
{

    int index = 0;

    while(string[index] != '\0')
    {
        if(string[index] == charToDel){
            string[index] = string[index+1];
        }

        index++;
    }

    printf("%s", string);
}

int main(void)
{

    char string[] = "Hello world";

    del(string, 'l');

    return 0;
}

I want to make this program without pointers. Just plain simple code.

I added another while loop that moves every character in the loop to the left but it doesn't seem to work since the output is just plain blank.

int index = 0;


    while(string[index] != '\0')
       {
           if(string[index] == charToDel)
           {
                while(string[index] != '\0')
                {
                    string[index] = string[index+1];
                }

           }

           index++;
       }

    printf("%s", string);
}

Johathan Leffler's Method?

        char newString[100];

    int index = 0;
    int i = 0;

    while(string[index] != '\0')
       {
           if(string[index] != charToDel)
           {
                newString[i] = string[index];

                index++;
                i++;

           }
           i++;
           index++;
       }

    printf("%s", newString);
}

This gives me a lot of weird characters...

解决方案

char const *in = string;
char *out = string;

while (*in) {
    if (*in != charToDel)
        *out++ = *in;
    ++in;
}

*out = '\0';

or without pointers

size_t in = 0;
size_t out = 0;

while (string[in]) {
    if (string[in] != charToDel)
         string[out++] = string[in];
    ++in;
}

string[out] = '\0';

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

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