C 循环未运行
我正在尝试将文件中的单词与用户输入进行比较。C 循环未运行,c,C,我正在尝试将文件中的单词与用户输入进行比较。 一切都已完成,但第二个循环仍不运行?有没有可能查一下 基本上我想做的是:就像在我的text.file中有一个单词“my”。现在我想通过比较text.txt文件“my”和他句子中用户输入的“my”来说明它是一个代词 Test.txt文件: 我们 我 我的 你 正在尝试将test.txt文件的字符串与代码字符串进行比较 #include <stdio.h> #include <string.h> #define FNAME "t
一切都已完成,但第二个循环仍不运行?有没有可能查一下 基本上我想做的是:就像在我的text.file中有一个单词“my”。现在我想通过比较text.txt文件“my”和他句子中用户输入的“my”来说明它是一个代词 Test.txt文件: 我们
我
我的
你 正在尝试将test.txt文件的字符串与代码字符串进行比较
#include <stdio.h>
#include <string.h>
#define FNAME "test.txt" /* if you need constants, define them */
#define NOUN "nouns.txt"
#define VERB "verbs.txt"
#define TENC 10
#define MAXC 100
int main (void) {
int c = 0, i = 0, ndx = 0, /* misc. counters & index */
fwords = 0, /* number of words in file */
uwords = 0; /* number of words from user */
char arr[MAXC] = "", /* storage for file contents */
str1[MAXC] = "", /* storage for user string */
wordsfile[TENC][TENC] = { "" }, /* separated words from file */
wordsnoun[TENC][TENC] = { "" },
wordsverb[TENC][TENC] = { "" },
wordsuser[TENC][TENC] = { "" }; /* separated words from user */
//wordsnoun[] = { "dog", "fleas" }; /* nouns - example */
size_t len = 0, /* string length */
nnouns = sizeof wordsnoun / sizeof *wordsnoun,
nverbs = sizeof wordsverb / sizeof *wordsverb; /* no. nouns */
FILE *fp = fopen (FNAME, "r");
if (fp == NULL) { /* validate file open for reading */
perror (FNAME);
return 1;
}
printf ("\n Reading from '%s' and separating into words by line :\n"
" --------------------------------------------------------\n\n",
FNAME);
/* read characters from file into 'arr' and separate into words
* by newline into 'wordsfile'. nul-terminate each word. protect
* all array bounds and against non-POSIX eof.
*/
while (ndx < MAXC && fwords < TENC && (c = fgetc(fp)) != EOF) {
if (c == '\n') { /* separate words in file by line */
wordsfile[fwords++][i] = 0; /* nul-terminate */
i = 0; /* reset words character count */
}
else /* assign char to both wordsfile & arr */
wordsfile[fwords][i++] = arr[ndx++] = c;
if (i >= TENC) { /* protect array bounds */
fprintf (stderr, "error: wordsfile - word too long.\n");
return 1;
}
}
if (i) /* handle non-POSIX file ending (no '\n') */
wordsfile[fwords++][i] = 0; /* nul-terminate */
fclose (fp); /* close the file - done with it */
FILE *np = fopen (NOUN, "r");
if (np == NULL) { /* validate file open for reading */
perror (NOUN);
return 1;
}
printf ("\n Reading from '%s' and separating into words by line :\n"
" --------------------------------------------------------\n\n",
NOUN);
/* read characters from file into 'arr' and separate into words
* by newline into 'wordsfile'. nul-terminate each word. protect
* all array bounds and against non-POSIX eof.
*/
while (ndx < MAXC && fwords < TENC && (c = fgetc(fp)) != EOF) {
if (c == '\n') { /* separate words in file by line */
wordsnoun[fwords++][i] = 0; /* nul-terminate */
i = 0; /* reset words character count */
}
else /* assign char to both wordsfile & arr */
wordsnoun[fwords][i++] = arr[ndx++] = c;
if (i >= TENC) { /* protect array bounds */
fprintf (stderr, "error: wordsnoun - word too long.\n");
return 1;
}
}
if (i) /* handle non-POSIX file ending (no '\n') */
wordsnoun[fwords++][i] = 0; /* nul-terminate */
fclose (np); /* close the file - done with it */
FILE *vp = fopen (VERB, "r");
if (vp == NULL) { /* validate file open for reading */
perror (VERB);
return 1;
}
printf ("\n Reading from '%s' and separating into words by line :\n"
" --------------------------------------------------------\n\n",
VERB);
/* read characters from file into 'arr' and separate into words
* by newline into 'wordsfile'. nul-terminate each word. protect
* all array bounds and against non-POSIX eof.
*/
while (ndx < MAXC && fwords < TENC && (c = fgetc(fp)) != EOF) {
if (c == '\n') { /* separate words in file by line */
wordsverb[fwords++][i] = 0; /* nul-terminate */
i = 0; /* reset words character count */
}
else /* assign char to both wordsfile & arr */
wordsverb[fwords][i++] = arr[ndx++] = c;
if (i >= TENC) { /* protect array bounds */
fprintf (stderr, "error: wordsverb - word too long.\n");
return 1;
}
}
if (i) /* handle non-POSIX file ending (no '\n') */
wordsverb[fwords++][i] = 0; /* nul-terminate */
fclose (vp); /* close the file - done with it */
for (i = 0; i < fwords; i++) /* output words from file */
printf ("wordsfile[%d] : %s\n", i, wordsnoun[i]);
printf ("\n Split string by space into words :\n"
" --------------------------------------\n"
" Input a string : ");
if (fgets (str1, MAXC, stdin) == NULL) { /* validate input */
fprintf (stderr, "error: user canceled input.\n");
return 1;
}
len = strlen (str1); /* validate string length */
if (len + 1 == MAXC && str1[len - 1] != '\n') {
fprintf (stderr, "error: string too long.\n");
return 1;
}
/* separate str1 into words on space ' '.
* this is the same approach as used on wordsfile.
* (note: you can use strtok for this purpose)
*/
for (i = 0, c = 0; c < (int)len; c++) {
if (str1[c] == ' ' || str1[c] == '\n') {
wordsuser[uwords++][i] = 0;
i = 0;
}
else /* assign char to wordsuser */
wordsuser[uwords][i++] = str1[c];
if (i >= TENC) { /* protect array bounds */
fprintf (stderr, "error: wordsuser - word too long.\n");
return 1;
}
}
if (i) /* handle last word in string */
wordsuser[uwords++][i] = 0; /* nul-terminate */
putchar ('\n');
for (i = 0; i < uwords; i++) /* output words from file */
printf ("wordsuser[%d] : %s\n", i, wordsuser[i]);
printf ("\n Determine if words entered by user are pronouns :\n"
" ---------------------------------------------------\n\n");
/* cycle through each word in user input comparing to words from file.
* if match found, output words as pronoun, otherwise output as
* not a pronoun.
*/
for (i = 0; i < uwords; i++) { /* for each word in wordsuser */
printf (" %-10s - ", wordsuser[i]); /* output the word */
for (c = 0; c < fwords; c++) { /* for each word in wordsfile */
/* do they match any word in wordsfile (pronouns) ? */
if (strcmp (wordsuser[i], wordsfile[c]) == 0) {
printf ("pronoun.\n");
goto nextword;
}
}
for (c = 0; c < (int)nnouns; c++) /* do they match a noun? */
if (strcmp (wordsuser[i], wordsnoun[c]) == 0) {
printf ("noun.\n");
goto nextword;
}
for (c = 0; c < (int)nverbs; c++) /* do they match a noun? */
if (strcmp (wordsuser[i], wordsverb[c]) == 0) {
printf ("verb.\n");
goto nextword;
}
/*
* ALL your remaining tests go here!
*/
printf ("unclassified.\n");
nextword:;
}
return 0;
}
#包括
#包括
#定义FNAME“test.txt”/*如果需要常量,请定义它们*/
#定义名词“nomes.txt”
#定义动词“verbs.txt”
#定义TENC 10
#定义MAXC 100
内部主(空){
int c=0,i=0,ndx=0,/*杂项计数器和索引*/
fwords=0,/*文件中的字数*/
uwords=0;/*来自用户的字数*/
char arr[MAXC]=“”,/*文件内容存储*/
str1[MAXC]=“”,/*用户字符串的存储*/
words文件[TENC][TENC]={“},/*从文件中分隔出单词*/
wordsnoun[TENC][TENC]={“},
wordsverb[TENC][TENC]={“},
wordsuser[TENC][TENC]={“};/*与用户分隔的单词*/
//wordsnoun[]={“狗”,“跳蚤”};/*名词-示例*/
大小长度=0,/*字符串长度*/
nnouns=sizeof wordsnoun/sizeof*wordsnoun,
nverbs=sizeof words-verb/sizeof*words-verb;/*无名词*/
文件*fp=fopen(FNAME,“r”);
如果(fp==NULL){/*验证文件是否打开以进行读取*/
佩罗尔(FNAME);
返回1;
}
printf(“\n从'%s'读取并按行分隔为单词:\n”
“--------------------------------------\n\n”,
FNAME);
/*将文件中的字符读入“arr”并分隔为单词
*通过换行到“wordsfile”。nul终止每个单词。保护
*所有数组边界和非POSIX eof。
*/
而(ndx=TENC){/*保护数组边界*/
fprintf(stderr,“错误:words文件-单词太长。\n”);
返回1;
}
}
if(i)/*处理非POSIX文件结尾(否“\n”)*/
字文件[fwords++][i]=0;/*nul终止*/
fclose(fp);/*关闭文件-完成*/
FILE*np=fopen(名词,“r”);
如果(np==NULL){/*验证文件是否打开以进行读取*/
perror(名词);
返回1;
}
printf(“\n从'%s'读取并按行分隔为单词:\n”
“--------------------------------------\n\n”,
名词);
/*将文件中的字符读入“arr”并分隔为单词
*通过换行到“wordsfile”。nul终止每个单词。保护
*所有数组边界和非POSIX eof。
*/
而(ndx=TENC){/*保护数组边界*/
fprintf(stderr,“错误:wordsnoun-单词太长。\n”);
返回1;
}
}
if(i)/*处理非POSIX文件结尾(否“\n”)*/
wordsnoun[fwords++][i]=0;/*num终止*/
fclose(np);/*关闭文件-完成*/
FILE*vp=fopen(动词“r”);
如果(vp==NULL){/*验证文件是否打开以进行读取*/
佩罗(动词);
返回1;
}
printf(“\n从'%s'读取并按行分隔为单词:\n”
“--------------------------------------\n\n”,
动词);
/*将文件中的字符读入“arr”并分隔为单词
*通过换行到“wordsfile”。nul终止每个单词。保护
*所有数组边界和非POSIX eof。
*/
而(ndx=TENC){/*保护数组边界*/
fprintf(stderr,“错误:wordsverb-单词太长。\n”);
返回1;
}
}
if(i)/*处理非POSIX文件结尾(否“\n”)*/
wordsverb[fwords++][i]=0;/*nul终止*/
fclose(vp);/*关闭文件-完成*/
对于(i=0;i#include <stdio.h>
#include <string.h>
#define FNAME "test.txt" /* if you need constants, define them */
#define TENC 10
#define MAXC 100
int main (void) {
int c = 0, i = 0, ndx = 0, /* misc. counters & index */
fwords = 0, /* number of words in file */
uwords = 0; /* number of words from user */
size_t len = 0; /* string length */
char arr[MAXC] = "", /* storage for file contents */
str1[MAXC] = "", /* storage for user string */
wordsfile[TENC][TENC] = { "" }, /* separated words from file */
wordsuser[TENC][TENC] = { "" }; /* separated words from user */
FILE *fp = fopen (FNAME, "r");
if (fp == NULL) { /* validate file open for reading */
perror (FNAME);
return 1;
}
printf ("\n Reading from '%s' and separating into words by line :\n"
" --------------------------------------------------------\n\n",
FNAME);
/* read characters from file into 'arr' and separate into words
* by newline into 'wordsfile'. nul-terminate each word. protect
* all array bounds and against non-POSIX eof.
*/
while (ndx < MAXC && fwords < TENC && (c = fgetc(fp)) != EOF) {
if (c == '\n') { /* separate words in file by line */
wordsfile[fwords++][i] = 0; /* nul-terminate */
i = 0; /* reset words character count */
}
else /* assign char to both wordsfile & arr */
wordsfile[fwords][i++] = arr[ndx++] = c;
if (i >= TENC) { /* protect array bounds */
fprintf (stderr, "error: wordsfile - word too long.\n");
return 1;
}
}
if (i) /* handle non-POSIX file ending (no '\n') */
wordsfile[fwords++][i] = 0; /* nul-terminate */
fclose (fp); /* close the file - done with it */
for (i = 0; i < fwords; i++) /* output words from file */
printf ("wordsfile[%d] : %s\n", i, wordsfile[i]);
printf ("\n Split string by space into words :\n"
" --------------------------------------\n"
" Input a string : ");
if (fgets (str1, MAXC, stdin) == NULL) { /* validate input */
fprintf (stderr, "error: user canceled input.\n");
return 1;
}
len = strlen (str1); /* validate string length */
if (len + 1 == MAXC && str1[len - 1] != '\n') {
fprintf (stderr, "error: string too long.\n");
return 1;
}
/* separate str1 into words on space ' '.
* this is the same approach as used on wordsfile.
* (note: you can use strtok for this purpose)
*/
for (i = 0, c = 0; c < (int)len; c++) {
if (str1[c] == ' ' || str1[c] == '\n') {
wordsuser[uwords++][i] = 0;
i = 0;
}
else /* assign char to wordsuser */
wordsuser[uwords][i++] = str1[c];
if (i >= TENC) { /* protect array bounds */
fprintf (stderr, "error: wordsuser - word too long.\n");
return 1;
}
}
if (i) /* handle last word in string */
wordsuser[uwords++][i] = 0; /* nul-terminate */
putchar ('\n');
for (i = 0; i < uwords; i++) /* output words from file */
printf ("wordsuser[%d] : %s\n", i, wordsuser[i]);
printf ("\n Determine if words entered by user are pronouns :\n"
" ---------------------------------------------------\n\n");
/* cycle through each word in user input comparing to words from file.
* if match found, output words as pronoun, otherwise output as
* not a pronoun.
*/
for (i = 0; i < uwords; i++) { /* for each word in wordsuser */
int pronoun = 0; /* flag for pronoun found */
for (c = 0; c < fwords; c++) { /* for each word in wordsfile */
/* do they match ? */
if (strcmp (wordsuser[i], wordsfile[c]) == 0) {
printf (" %-10s - pronoun.\n", wordsuser[i]);
pronoun = 1;
}
}
if (!pronoun) /* if no match found */
printf (" %-10s - not a pronoun.\n", wordsuser[i]);
}
return 0;
}
$ ./bin/splitonspace
Reading from 'test.txt' and separating into words by line :
--------------------------------------------------------
wordsfile[0] : we
wordsfile[1] : I
wordsfile[2] : my
wordsfile[3] : you
Split string by space into words :
--------------------------------------
Input a string : I don't want my dog to have fleas do you
wordsuser[0] : I
wordsuser[1] : don't
wordsuser[2] : want
wordsuser[3] : my
wordsuser[4] : dog
wordsuser[5] : to
wordsuser[6] : have
wordsuser[7] : fleas
wordsuser[8] : do
wordsuser[9] : you
Determine if words entered by user are pronouns :
---------------------------------------------------
I - pronoun.
don't - not a pronoun.
want - not a pronoun.
my - pronoun.
dog - not a pronoun.
to - not a pronoun.
have - not a pronoun.
fleas - not a pronoun.
do - not a pronoun.
you - pronoun.
char arr[MAXC] = "", /* storage for file contents */
...
*wordsnoun[] = { "dog", "fleas" }; /* nouns - example */
size_t len = 0, /* string length */
nnouns = sizeof wordsnoun / sizeof *wordsnoun; /* no. nouns */
/* cycle through each word in user input comparing to classification words.
* if match found, output classification for word, otherwise if no match
* is found, output "unclassified".
*/
for (i = 0; i < uwords; i++) { /* for each word in wordsuser */
printf (" %-10s - ", wordsuser[i]); /* output the word */
for (c = 0; c < fwords; c++) { /* for each word in wordsfile */
/* do they match any word in wordsfile (pronouns) ? */
if (strcmp (wordsuser[i], wordsfile[c]) == 0) {
printf ("pronoun.\n");
goto nextword;
}
}
for (c = 0; c < (int)nnouns; c++) /* do they match a noun? */
if (strcmp (wordsuser[i], wordsnoun[c]) == 0) {
printf ("noun.\n");
goto nextword;
}
/*
* ALL your remaining tests go here!
*/
printf ("unclassified.\n");
nextword:;
}
$ ./bin/splitonspace
Reading from 'test.txt' and separating into words by line :
--------------------------------------------------------
wordsfile[0] : we
wordsfile[1] : I
wordsfile[2] : my
wordsfile[3] : you
Split string by space into words :
--------------------------------------
Input a string : I don't want my dog to have fleas do you
String entered : I don't want my dog to have fleas do you
wordsuser[0] : I
wordsuser[1] : don't
wordsuser[2] : want
wordsuser[3] : my
wordsuser[4] : dog
wordsuser[5] : to
wordsuser[6] : have
wordsuser[7] : fleas
wordsuser[8] : do
wordsuser[9] : you
Determine if words entered by user are pronouns :
---------------------------------------------------
I - pronoun.
don't - unclassified.
want - unclassified.
my - pronoun.
dog - noun.
to - unclassified.
have - unclassified.
fleas - noun.
do - unclassified.
you - pronoun.