在这段代码中,我用换行符 "\n" 的不同来分割字符串。为此,我可以使用 strtok ,但我不会使用它。我只想从字符串中得到一个特定的子字符串。喜欢
"\n"
strtok
预期结果:
printf("%s\n", word[0]); Hello World printf("%s\n", word[1]); How are you?
给出的结果:
printf("%s\n", word[0]); Hello World How are you? printf("%s\n", word[1]); How are you?
我不知道这段代码有什么问题,因为它给了我这样的结果。
码
int main(){ char words[10][10]; int indexCtr = 0, wordIndex = 0, totalWords = 0; char string[100] = "Hello World\nhow are you?"; for(indexCtr = 0; indexCtr <= strlen(string); indexCtr++){ if(string[indexCtr] == "\n" || string[indexCtr] == '\0'){ words[totalWords][wordIndex] = '\0'; totalWords++; wordIndex = 0; else{