gpt4 book ai didi

c - 如何在C中将一个字符变成一个单词

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:38 25 4
gpt4 key购买 nike

我目前正在从一个文件中收集输入,但我的程序将每个字母而不是每个单词分隔到 char 数组中。如何更改我的代码以获取每个单词?

char c, fileName[20];
FILE *f;

void getFile() {
f = fopen(fileName, "r");

while((c = fgetc(f)) != EOF) {
printf("%c",c);
}
fclose(f);
}

最佳答案

您可以将扫描集与 fscanf 或 sscanf 一起使用。此扫描集 %29[a-zA-Z] 读取小写和大写英文字符,并在遇到不在集合中的字符时停止。 29 限制了要读取的最大字符数,以免覆盖缓冲区 word[30]。当 fscanf 失败时,else 将从文件中读取一个字符并让 fscanf 再次尝试读取另一个单词。
这也是使用命令行传入文件读取为argv[1]。

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[])
{
char word[30] = {'\0'};
int ch = 0;
FILE *pf = NULL;

if ( argc != 2) {//command requires program name and a file name
printf ( "useage: program filename\n");
return 1;
}

if ( ( pf = fopen ( argv[1], "r")) == NULL) {
perror ( "could not open file");
return 1;
}

while ( 1) {
if ( ( fscanf ( pf, "%29[a-zA-Z]", word)) == 1) {
printf ( "%s\n", word);
}
else {
if ( ( ch = fgetc ( pf)) == EOF) {//read one character and check for end of file
break;
}
//could do something here with the value of ch if needed
}
}
printf ( "--DONE--\n");

return 0;
}

这将为每个单词分配一个数组。添加单词时,使用 realloc 扩展数组。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char *argv[])
{
char **words = NULL;//pointer for words
char **temp = NULL;
char word[30] = {'\0'};
int ch = 0;
int each = 0;
int found = 0;
int count = 0;
int wordsize = 0;
FILE *pf = NULL;

if ( argc != 2) {//command requires program name and a file name
printf ( "useage: program filename\n");
return 1;
}

if ( ( pf = fopen ( argv[1], "r")) == NULL) {
perror ( "could not open file");
return 1;
}

while ( 1) {
if ( ( fscanf ( pf, "%29[a-zA-Z]", word)) == 1) {
found = 0;
for ( each = 0; each < wordsize; each++) {
if ( strcmp ( words[each], word) == 0) {
found = 1;
break;
}
}
if ( found == 0) {
wordsize += 1;// increment number of words
temp = realloc ( words, wordsize * sizeof ( char *));//reallocate for another word
if ( temp != NULL) {
words = temp;
words[wordsize - 1] = malloc ( strlen ( word) + 1);//malloc for the word itself
if ( words[wordsize - 1] != NULL) {
strcpy ( words[wordsize - 1], word);
}
else {
printf ( "malloc failed\n");
wordsize -= 1;
break;
}
}
else {
printf ( "realloc failed\n");
wordsize -= 1;
break;
}
}
printf ( "%s\n", word);
}
else {
if ( ( ch = fgetc ( pf)) == EOF) {//read one character and check for end of file
break;
}
//something could be done with ch if needed
}
}
printf ( "--DONE Reading file--\n");
for ( each = 0; each < wordsize; each++) {// print each word
printf ( "%s\n", words[each]);
}
count = 0;
printf ( "Enter a word to search for\n");
if ( ( scanf ( "%29[a-zA-Z]", word)) == 1) {
for ( each = 0; each < wordsize; each++) {
if ( strcmp ( words[each], word) == 0) {
printf ( "Found %s at index %d\n" word, each);
count++;
}
}
printf ( "Found %s %d times\n" word, count);
}
for ( each = 0; each < wordsize; each++) {//release memory
free ( words[each]);
}
free ( words);


return 0;
}

关于c - 如何在C中将一个字符变成一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28593724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com