2013-11-24 18 views
0

所以,如果我在C以下的字符數組:刪除字符數組中的第一個令牌,並保持休息用C

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

我如何刪除「」令牌,但存儲休息「BC 「在一個字符指針?

到目前爲止,我已經實現了不起作用下面的方法:

char* removeAFromABC(char* a, char* abc) { 
    char* abcWithoutA[MAXIMUM_LINE_LENGTH + 1]; 

    int numberOfCharsInA = strlen(a); 

    strcpy(abcWithoutA, (abc + numberOfCharsInA)); 
    return abcWithoutA; 
} 
+1

怎麼樣:'字符* str_minus_sw =(your_array + 2);'? –

+0

但問題是,「SW」不會永遠是第一個字符 –

+0

代碼很簡單。 –

回答

0

編輯海報後,答案已經澄清他所需要的:

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 */ 
}  
+0

我很抱歉,但我編輯了我的問題,使其更清楚。 –

+0

回答更新:) –

0

使用的strtok()來標記你的字符串,其中包括「SW」令牌。在你的循環使用的strcmp(),看看是否令牌「SW」,如果是這樣,忽略它。

或者,如果你知道「SW」總是在你的字符串中的前兩個字符,只是你的記號化字符串開始STR + 2跳過這些字符。

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

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/

+0

我很抱歉,但我編輯了我的問題以使其更清楚。 –

相關問題