我从 6 月 1 日开始学习 C 语言,很抱歉我的英语不好。
我正在编写程序来从键盘搜索和计算句子中的一些介词,如“at、in 和 on”。我目前正在编写这样的代码。但是,我不能在“char word”中输入字符串。也许,我无法正确使用某些功能,但我不知道问题出在哪里。有人可以帮我吗??
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char word[50][30];
char prep_at[] = "at";
char prep_by[] = "by";
char prep_for[] = "for";
char prep_in[] = "in";
char prep_on[] = "on";
char prep_of[] = "of";
char prep_to[] = "to";
char prep_with[] = "with";
int count[6] = {0};
void GetLine(char str[]);
void GetWord(char str[]);
void ToLower(char str[]);
int main(void)
{
char str[512];//最初のテキスト
int i = 0;
GetLine(str);
GetWord(str);
ToLower(str);
printf("%s",word[1][1]);
while (word != '\0'){
if (strcmp(word[i],prep_at) == 0)
count[0]++;
i++;
}
printf("%d",count[0]);
}
void GetLine(char str[])
{
printf("文字列を入力:");
scanf("%s",str);
}
void GetWord(char str[]){
int i = 0;
char *token;
token = strtok(str," ");
while(token != NULL){
strcpy(word[i],token);
//printf("%s\n",word[i]); //単語を取り出せたか確認用
token = strtok(NULL," ");
i++;
}
}
void ToLower(char str[]) {
int i = 0;
while (str[i] != '\0' ) { //文字列の入っているセンテンスがEOSじゃなければループ
str[i] = tolower((unsigned char)str[i]);
// printf("%s\n",word[i]);
i++;
}
}