- 論壇徽章:
- 0
|
本帖最后由 tlocean 于 2010-10-07 22:09 編輯
源代碼 如下:
環(huán)境:ubuntu + gcc
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void my()
{
char s[] = "我是中國人/測試用/abcdefghijkl/[測試用]";
char *my_p = NULL;
const char *delim = "/";
char *pointer;
my_p = strtok(s,delim);
printf("______%s______this is the first_time!\n",my_p);
while(pointer != NULL)
{
strcat(my_p,pointer);
pointer = strtok(NULL,delim);
}
printf("______%s______this is the final_time!\n",my_p);
}
int main(void)
{
my();
return 0;
}
輸出結(jié)果如下:
______我是中國人______this is the first_time!
______我是中國人測試用abcdefghijkl[測試用]______this is the final_time!
說明:我的本意是想寫段代碼來把字符串中不需要的字符去除掉,這個功能是實(shí)現(xiàn)了!
但是有個關(guān)于while循環(huán)的問題:1)while循環(huán)不是會先判斷里面的邏輯表達(dá)式才決定是否執(zhí)行里面的語句嗎?可是我上面的語句while(pointer != NULL)此時我覺得指針pointer應(yīng)該為空啊
因?yàn)樵谶@之前我沒有給pointer賦任何值啊 那么while里面的語句應(yīng)該不執(zhí)行才對啊!怎么現(xiàn)在功能變成了先執(zhí)行里面的語句再判斷呢?也不知道我說的是否明白 現(xiàn)在很糾結(jié) 希望各位能解答一下!
謝謝! |
|