删除char数组中的第一个标记,并将其余标记保留在C中

所以如果我在C中有以下char数组:

"abc" // where "a", "b", and "c" can be char arrays of any length and the // space between them can be of any length 

如何删除“a”标记但将其余的“bc”存储在char指针中?

到目前为止,我已经实现了以下不起作用的方法:

 char* removeAFromABC(char* a, char* abc) { char* abcWithoutA[MAXIMUM_LINE_LENGTH + 1]; int numberOfCharsInA = strlen(a); strcpy(abcWithoutA, (abc + numberOfCharsInA)); return abcWithoutA; } 

海报澄清了他的需求后编辑的答案:

 char* removeAFromABC(char* a, char* abc) { char *t; t = strstr(abc, a); /* Find A string into ABC */ if (t) /* Found? */ for (t+=strlen(a);(*t)==' ';t++); /* Then advance to the fist non space char */ return t; /* Return pointer to BC part of string, or NULL if A couldn't be found */ } 

使用strtok()来标记字符串,包括’sw’标记。 在你的循环中使用strcmp()来查看令牌是否为“sw”,如果是,则忽略它。

或者,如果您知道’sw’始终是字符串中的前两个字符,则只需将字符串标记为从str + 2开始跳过这些字符。

  #include  #include int main() { char a[20]="sw $s2, 0($s3)"; char b[20]; // char *b=NULL; b=(a+5); Can also be done. strcpy(b,(a+5)); printf("%s",b); } 

或者上面提到的strtok方法。 有关strtok的信息,请访问http://www.cplusplus.com/reference/cstring/strtok/