C 选择性地删除字符串中的特定字符
假设我有一个字符串,看起来像这样:C 选择性地删除字符串中的特定字符,c,arrays,string,text,char,C,Arrays,String,Text,Char,假设我有一个字符串,看起来像这样: "value" "some other value" "other value" "some value" 我的目标是有选择地删除空白,如下所示: "value""some other value""other value""some value" 使空格仅保留在引号中包含的字符串中的: "some other value" 我有以下功能: void rmChar(char *str, char c) { char *src, *
"value" "some other value" "other value" "some value"
我的目标是有选择地删除空白,如下所示:
"value""some other value""other value""some value"
使空格仅保留在引号中包含的字符串中的:
"some other value"
我有以下功能:
void rmChar(char *str, char c)
{
char *src, *dest;
src = dest = str;
while(*src != '\0')
{
if (*src != c)
{
*dest = *src;
dest++;
}
src++;
}
*dest = '\0';
}
这将删除str中所有出现的charc,我想我应该使用更多的条件表达式,只有在某些情况发生时才进行删除
有什么线索吗 在字符串上进行迭代的循环必须跟踪当前是否正在查看带引号的字符串中的字符,然后使用该信息仅在适当时删除 为了跟踪这些信息,您可以使用一个附加变量,该变量会在每次出现
“
时更新
我只是想这么做。下面是我的程序 注意:这可能不是一个有效的程序(时间或空间复杂度不好),但是它可以完成您正在尝试做的事情(如果我正确理解您的问题的话) 注意我在这段代码中也使用了malloc()。如果您在不使用任何其他字符串的情况下更改原始字符串的内容,您将不会使用它。但我从您的问题中了解到,您正在创建一个新字符串,其中包含删除空格后原始字符串的值
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void rmChar(char *,char, int );
int main()
{
char string[200] = "\"This is a value\" \"and another value\" \"value2 this\"";
char c;
c = '"';
printf("%s\n",string);
int len = strlen(string);
/*Pass the address of the stringi, char c, and the length of the string*/
/*Length of the string will be required for malloc() inside function rmChar()*/
rmChar(string, c, len);
return 0;
}
void rmChar(char *str,char c, int len)
{
char *dest1, *dest2;
char *src = str;
int removeFlag = 0; /* You will remove all the spaces ' ' that come after removeFlag is odd*/
dest1 = malloc(len);
dest2 = dest1;
while(*str != '\0')
{
if(*str == c)
{
removeFlag++;
if (removeFlag %2 == 0)
{
/* This is required because every 2nd time you get a " removeFlag is increased so next if is NOT true*/
*dest2 = *str;
dest2++;
}
}
if ((removeFlag % 2) == 1)
{
*dest2 = *str;
dest2++;
}
str++;
}
*dest2 = '\0';
printf("%s\n", dest1);
/* If you want to copy the string without spaces to the original string uncomment below line*/
//strcpy(src, dest1);
free(dest1);
}
#包括
#包括
#包括
void rmChar(char*,char,int);
int main()
{
字符字符串[200]=“\”这是一个值“\”和另一个值“\”值2这\”;
字符c;
c=“”;
printf(“%s\n”,字符串);
int len=strlen(字符串);
/*传递字符串i的地址、字符c和字符串的长度*/
/*函数rmChar()内的malloc()需要字符串的长度*/
rmChar(字符串,c,len);
返回0;
}
void rmChar(char*str、char c、int len)
{
字符*dest1,*dest2;
char*src=str;
int removeFlag=0;/*您将删除removeFlag为奇数之后的所有空格“”*/
dest1=malloc(len);
dest2=dest1;
而(*str!='\0')
{
如果(*str==c)
{
removeFlag++;
如果(removeFlag%2==0)
{
/*这是必需的,因为每第二次您获得一个“removeFlag”时,它就会增加,所以next if不是true*/
*dest2=*str;
dest2++;
}
}
如果((removeFlag%2)==1)
{
*dest2=*str;
dest2++;
}
str++;
}
*dest2='\0';
printf(“%s\n”,dest1);
/*如果要将不带空格的字符串复制到第行下方的原始字符串,请取消注释*/
//strcpy(src,dest1);
免费(1);
}
您还需要一个变量来用作某种标志,该标志指示“您需要删除空格”。然后您可以在
if()
语句中以某种方式使用该标志。这里int-removeFlag
是我使用的标志 谢谢你,好先生!我已经设法使用奇偶校验(和你一样)和更多的测试(对于更复杂的字符串)来完成它。@AlexandruDinu很高兴它起到了作用。是的,我想有很多方法可以做到。如果您研究了所有这些方法,那么就可以使用最好的方法(使用最少的变量或最少的代码行)。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void rmChar(char *,char, int );
int main()
{
char string[200] = "\"This is a value\" \"and another value\" \"value2 this\"";
char c;
c = '"';
printf("%s\n",string);
int len = strlen(string);
/*Pass the address of the stringi, char c, and the length of the string*/
/*Length of the string will be required for malloc() inside function rmChar()*/
rmChar(string, c, len);
return 0;
}
void rmChar(char *str,char c, int len)
{
char *dest1, *dest2;
char *src = str;
int removeFlag = 0; /* You will remove all the spaces ' ' that come after removeFlag is odd*/
dest1 = malloc(len);
dest2 = dest1;
while(*str != '\0')
{
if(*str == c)
{
removeFlag++;
if (removeFlag %2 == 0)
{
/* This is required because every 2nd time you get a " removeFlag is increased so next if is NOT true*/
*dest2 = *str;
dest2++;
}
}
if ((removeFlag % 2) == 1)
{
*dest2 = *str;
dest2++;
}
str++;
}
*dest2 = '\0';
printf("%s\n", dest1);
/* If you want to copy the string without spaces to the original string uncomment below line*/
//strcpy(src, dest1);
free(dest1);
}