C语言 如何从C中的字符串中删除给定索引处的字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5457608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove the character at a given index from a string in C?
提问by Favolas
How do I remove a character from a string?
如何从字符串中删除一个字符?
If I have the string "abcdef"and I want to remove "b"how do I do that?
如果我有字符串"abcdef"并且我想删除"b"我该怎么做?
Removing the firstcharacter is easy with this code:
使用以下代码可以轻松删除第一个字符:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char word[] = "abcdef";
char word2[10];
strcpy(word2,&word[1]);
printf("%s\n", word2);
return 0;
}
and
和
strncpy(word2,word,strlen(word)-1);
will give me the string without the lastcharacter, but I still didn't figure out how to remove a char in the middleof a string.
会给我没有最后一个字符的字符串,但我仍然不知道如何删除字符串中间的字符。
回答by stacker
memmovecan handle overlapping areas, I would try something like that (not tested, maybe +-1 issue)
memmove可以处理重叠区域,我会尝试类似的东西(未测试,可能是 +-1 问题)
char word[] = "abcdef";
int idxToDel = 2;
memmove(&word[idxToDel], &word[idxToDel + 1], strlen(word) - idxToDel);
Before: "abcdef"
前: "abcdef"
After: "abdef"
后: "abdef"
回答by Fabio Cabral
Try this :
尝试这个 :
void removeChar(char *str, char garbage) {
char *src, *dst;
for (src = dst = str; *src != 'int main(void) {
char* str = malloc(strlen("abcdef")+1);
strcpy(str, "abcdef");
removeChar(str, 'b');
printf("%s", str);
free(str);
return 0;
}
'; src++) {
*dst = *src;
if (*dst != garbage) dst++;
}
*dst = '>>acdef
';
}
Test program:
测试程序:
void RemoveChars(char *s, char c)
{
int writer = 0, reader = 0;
while (s[reader])
{
if (s[reader]!=c)
{
s[writer++] = s[reader];
}
reader++;
}
s[writer]=0;
}
Result:
结果:
char a[]="string";
int toBeRemoved=2;
memmove(&a[toBeRemoved],&a[toBeRemoved+1],strlen(a)-toBeRemoved);
puts(a);
回答by tBlabs
My way to remove all specified chars:
我删除所有指定字符的方法:
int chartoremove = 1;
strncpy(word2,word,chartoremove);
strncpy(((char*)word2)+chartoremove,((char*)word)+chartoremove+1,strlen(word)-1-chartoremove);
回答by SaravananKS
strcpy(&str[idx_to_delete], &str[idx_to_delete + 1]);
Try this . memmovewill overlap it. Tested.
尝试这个 。memmo将重叠它。测试。
回答by unexpectedvalue
#include <stdio.h>
#include <string.h>
int main(){
char ch[15],ch1[15];
int i;
gets(ch); // the original string
for (i=0;i<strlen(ch);i++){
while (ch[i]==ch[i+1]){
strncpy(ch1,ch,i+1); //ch1 contains all the characters up to and including x
ch1[i]='/*
* delete one character from a string
*/
static void
_strdelchr( char *s, size_t i, size_t *a, size_t *b)
{
size_t j;
if( *a == *b)
*a = i - 1;
else
for( j = *b + 1; j < i; j++)
s[++(*a)] = s[j];
*b = i;
}
/*
* delete all occurrences of characters in search from s
* returns nr. of deleted characters
*/
size_t
strdelstr( char *s, const char *search)
{
size_t l = strlen(s);
size_t n = strlen(search);
size_t i;
size_t a = 0;
size_t b = 0;
for( i = 0; i < l; i++)
if( memchr( search, s[i], n))
_strdelchr( s, i, &a, &b);
_strdelchr( s, l, &a, &b);
s[++a] = 'char buf[100] = "abcdef";
char remove = 'b';
char* c;
if ((c = index(buf, remove)) != NULL) {
size_t len_left = sizeof(buf) - (c+1-buf);
memmove(c, c+1, len_left);
}
';
return l - a;
}
'; //removing x from ch1
strcpy(ch,&ch[i+1]); //(shrinking ch) removing all the characters up to and including x from ch
strcat(ch1,ch); //rejoining both parts
strcpy(ch,ch1); //just wanna stay classy
}
}
puts(ch);
}
Ugly as hell
丑到不行
回答by MD XF
Really surprised this hasn't been posted before.
真的很惊讶这之前没有发布过。
##代码##Pretty efficient and simple. strcpyuses memmoveon most implementations.
相当高效和简单。strcpy用于memmove大多数实现。
回答by Wassim Mohsni
Let's suppose that x is the "symbol" of the character you want to remove ,my idea was to divide the string into 2 parts:
假设 x 是您要删除的字符的“符号”,我的想法是将字符串分成 2 部分:
1st part will countain all the characters from the index 0 till (and including) the target character x.
第一部分将计算从索引 0 到(并包括)目标字符 x 的所有字符。
2nd part countains all the characters after x (not including x)
第二部分计算x之后的所有字符(不包括x)
Now all you have to do is to rejoin both parts.
现在您所要做的就是重新加入这两个部分。
回答by Dan the Man
The following will extends the problem a bit by removing from the first string argument any character that occurs in the second string argument.
下面将通过从第一个字符串参数中删除出现在第二个字符串参数中的任何字符来稍微扩展问题。
##代码##回答by presto8
Another solution, using memmove() along with index() and sizeof():
另一个解决方案,使用 memmove() 以及 index() 和 sizeof():
##代码##buf[] now contains "acdef"
buf[] 现在包含“acdef”
回答by David Heffernan
Use strcat()to concatenate strings.
使用strcat()连接字符串。
But strcat()doesn't allow overlapping so you'd need to create a new string to hold the output.
但strcat()不允许重叠,因此您需要创建一个新字符串来保存输出。

