C - sizeof 问题(以 strtok 为特色)
C - issues with sizeof (featuring strtok)
编辑:后来发现这个问题主要是因为我把sizeof搞糊涂了,换成 strlen 几乎是我的解决方案。我的回答(向下滚动)提供了一个不错但简单的例子 strtok 如果你也有兴趣的话。
所以我一直在尝试让一个程序运行,我输入一个用逗号分隔的单词列表,然后它会逐行输出这些单词,并删除所有空格。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define delim ","
int main() {
//variable declaration:
char words[100];
char *word;
char tempWord[100];
int n;
//gets input assinged to "words":
puts("\nEnter a list of words separated by commas.\n");
fgets(words, sizeof(words), stdin);
//sets up the first word in strtok
word = strtok(words, delim);
//loops so long as the word isn't null (reaching the last word)
while (word != NULL) {
puts("\n");
//checks if each character in the word is a space (and ignores them if they are)
for (n = 0; n < sizeof(word); ++n) {
//for some reason can't directly use word (probably because it's a pointer)
//so have to copy it to a temporary value
strcpy(tempWord, word);
//don't print if it's a space
if (!isspace(tempWord[n])) printf("%c", tempWord[n]);
}
//moves to next word
word = strtok(NULL, delim);
}
return(0);
}
通过输入 "LETS, FREAKINGG, GOOOOOOOOOOOO",我似乎遇到了一个问题:
(运行 程序):
Enter a list of words separated by commas.
(input) >>>LETS, FREAKINGG, GOOOOOOOOOOOO
LETS
FREAKIN
GOOOOOO
好像是根据第一个单词的大小,给后面的单词设置了不超过3个字符的限制。谁能解释为什么会这样?
谢谢@rici和@kaylum 回复!
由于某种原因,我之前在没有将单词复制到临时变量时遇到了一些错误,我不完全确定将它放在 for 循环中时我在想什么,但没有它就可以工作!只是一个非常小的脑洞...
我相信我的困惑是围绕 sizeof(),用 strlen() 替换是 完整修复为此!
这是修改后的(工作)代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define delim ","
int main() {
//variable declaration:
char words[100];
char *word;
int n;
//gets input assinged to "words":
puts("\nEnter a list of words separated by commas.\n");
fgets(words, sizeof(words), stdin);
//sets up the first word in strtok
word = strtok(words, delim);
//loops so long as the word isn't null (reaching the last word)
while (word != NULL) {
puts("\n");
//checks if each character in the word is a space (and ignores them if they are)
for (n = 0; n < strlen(word); ++n) {
//don't print if it's a space
if (!isspace(word[n])) printf("%c", word[n]);
}
//moves to next word
word = strtok(NULL, delim);
}
return(0);
}
和输出:
Enter a list of words separated by commas.
(input) >>>LETS, FREAKINGG, GOOOOOOOOOOOO
LETS
FREAKINGG
GOOOOOOOOOOOO
非常感谢您的帮助!
编辑:后来发现这个问题主要是因为我把sizeof搞糊涂了,换成 strlen 几乎是我的解决方案。我的回答(向下滚动)提供了一个不错但简单的例子 strtok 如果你也有兴趣的话。
所以我一直在尝试让一个程序运行,我输入一个用逗号分隔的单词列表,然后它会逐行输出这些单词,并删除所有空格。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define delim ","
int main() {
//variable declaration:
char words[100];
char *word;
char tempWord[100];
int n;
//gets input assinged to "words":
puts("\nEnter a list of words separated by commas.\n");
fgets(words, sizeof(words), stdin);
//sets up the first word in strtok
word = strtok(words, delim);
//loops so long as the word isn't null (reaching the last word)
while (word != NULL) {
puts("\n");
//checks if each character in the word is a space (and ignores them if they are)
for (n = 0; n < sizeof(word); ++n) {
//for some reason can't directly use word (probably because it's a pointer)
//so have to copy it to a temporary value
strcpy(tempWord, word);
//don't print if it's a space
if (!isspace(tempWord[n])) printf("%c", tempWord[n]);
}
//moves to next word
word = strtok(NULL, delim);
}
return(0);
}
通过输入 "LETS, FREAKINGG, GOOOOOOOOOOOO",我似乎遇到了一个问题:
(运行 程序):
Enter a list of words separated by commas.
(input) >>>LETS, FREAKINGG, GOOOOOOOOOOOO
LETS
FREAKIN
GOOOOOO
好像是根据第一个单词的大小,给后面的单词设置了不超过3个字符的限制。谁能解释为什么会这样?
谢谢@rici和@kaylum 回复!
由于某种原因,我之前在没有将单词复制到临时变量时遇到了一些错误,我不完全确定将它放在 for 循环中时我在想什么,但没有它就可以工作!只是一个非常小的脑洞...
我相信我的困惑是围绕 sizeof(),用 strlen() 替换是 完整修复为此!
这是修改后的(工作)代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define delim ","
int main() {
//variable declaration:
char words[100];
char *word;
int n;
//gets input assinged to "words":
puts("\nEnter a list of words separated by commas.\n");
fgets(words, sizeof(words), stdin);
//sets up the first word in strtok
word = strtok(words, delim);
//loops so long as the word isn't null (reaching the last word)
while (word != NULL) {
puts("\n");
//checks if each character in the word is a space (and ignores them if they are)
for (n = 0; n < strlen(word); ++n) {
//don't print if it's a space
if (!isspace(word[n])) printf("%c", word[n]);
}
//moves to next word
word = strtok(NULL, delim);
}
return(0);
}
和输出:
Enter a list of words separated by commas.
(input) >>>LETS, FREAKINGG, GOOOOOOOOOOOO
LETS
FREAKINGG
GOOOOOOOOOOOO
非常感谢您的帮助!