나는 토핑을 토큰 화하려고 시도하고 있으며, 여기에는 내 시도가 있습니다.포인터를 사용하여 문자열에 토큰 화하기
char new_str[1024];
void tokenize_init(const char str[]){//copy the string into global section
strcpy(new_str,str);
}
int i = 0;
char *tokenize_next() {
const int len = strlen(new_str);
for(; i <= len; i++) {
if (i == len) {
return NULL;
}
if ((new_str[i] >= 'a' && new_str[i] <= 'z') ||
(new_str[i] >= 'A' && new_str[i] <= 'Z')) {
continue;
}else {
new_str[i] = '\0';
i = i + 1;
return new_str;
}
}
return NULL;
}
//main function
int main(void) {
char sentence[] = "This is a good-sentence for_testing 1 neat function.";
printf("%s\n", sentence);
tokenize_init(sentence);
for (char *nt = tokenize_next();
nt != NULL;
nt = tokenize_next())
printf("%s\n",nt);
}
그러나 문장의 첫 번째 단어 ("This")를 인쇄 한 다음 중지합니다. 누군가 왜 저에게 말할 수 있습니까? 내 생각 엔 내 new_str은 영구적이지 않으며 main 함수가 tokenize_next()를 호출하면 new_str이 문장의 첫 단어가됩니다. 미리 감사드립니다.
'strtok()'를 사용하여 문자열을 토큰 화하는 특별한 이유가 있습니까? –
http://www.elook.org/programming/c/strtok.html 도움이 될 수 있습니다 –
'strsep'가 새로운 인기가 아닌가요? –